java 语音聊天核心代码
来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 16:38 点击:次
import java.io.*;
import javax.sound.sampled.*;
import java.net.*;
import javax.sound.sampled.*;
import java.net.*;
class Playback implements Runnable {
final int bufSize = 16384;
SourceDataLine line;
Thread thread;
Socket s;
SourceDataLine line;
Thread thread;
Socket s;
Playback(Socket s){//构造器 取得socket以获得网络输入流
this.s=s;
}
public void start() {
this.s=s;
}
public void start() {
thread = new Thread(this);
thread.setName("Playback");
thread.start();
}
thread.setName("Playback");
thread.start();
}
public void stop() {
thread = null;
}
thread = null;
}
public void run() {
AudioFormat format =new AudioFormat(8000,16,2,true,true);//AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian)
BufferedInputStream playbackInputStream;
BufferedInputStream playbackInputStream;
try {
playbackInputStream=new BufferedInputStream(new AudioInputStream(s.getInputStream(),format,2147483647));//封装成音频输出流,如果网络流是经过压缩的需在此加套解压流
}
catch (IOException ex) {
return;
}
playbackInputStream=new BufferedInputStream(new AudioInputStream(s.getInputStream(),format,2147483647));//封装成音频输出流,如果网络流是经过压缩的需在此加套解压流
}
catch (IOException ex) {
return;
}
DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);
try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufSize);
} catch (LineUnavailableException ex) {
return;
}
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufSize);
} catch (LineUnavailableException ex) {
return;
}
byte[] data = new byte[1024];//此处数组的大小跟实时性关系不大,可根据情况进行调整
int numBytesRead = 0;
line.start();
int numBytesRead = 0;
line.start();
while (thread != null) {
try{
numBytesRead = playbackInputStream.read(data);
line.write(data, 0,numBytesRead);
} catch (IOException e) {
break;
&n
try{
numBytesRead = playbackInputStream.read(data);
line.write(data, 0,numBytesRead);
} catch (IOException e) {
break;
&n
相关新闻>>
- 发表评论
-
- 最新评论 更多>>