class Capture implements Runnable{html
TargetDateLine line;//能够从中读取音频数据的某种类型的
java
DataLine
Thread thread;
网络
Socket socket;
socket
BufferedOutputStream captrueOutputStream;
ide
Captrue(Socket socket){//构造函数,取得socket
函数
this.socket=socket;
this
}
线程
public void start(){//启动线程
code
thread=new Thread(this);
orm
thread.setName(“Capture”);
thread.start();
}
public void stop(){//中止线程
thread=null;
}
public void run(){
try{
captrueOutputStream=new BufferedOutputStream(s.getOutputStream())//创建输出流
}catch(IOException e){
return;
}
AudioFormat format =new AudioFormat(8000,16,2,true,true);//AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian)
DataLine.Info info = new DataLine.Info(TargetDataLine.class,format);try {
line = (TargetDataLine) AudioSystem.getLine(info);
line.open(format, line.getBufferSize());
} catch (Exception ex) {
return;
}
byte[] data = new byte[1024];//此处的1024能够状况进行调整,应跟下面的1024应保持一致
int numBytesRead=0;
line.start();while (thread != null) {
numBytesRead = line.read(data, 0,128);//取数据(1024)的大小直接关系到传输的速度,通常越小越快,
try {
captrueOutputStream.write(data, 0, numBytesRead);//写入网络流
}
catch (Exception ex) {
break;
}
}line.stop();
line.close();
line = null;try { captrueOutputStream.flush(); captrueOutputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } }