Class Capture implements runnable{
Targetdateline line;//can read audio data from some kind ofDataLine
Thread thread;
Socket socket;
BufferedOutputStream captrueOutputStream;
Captrue(Socket socket){//构造函数,取得socket
?? this.socket=socket;
}
public void start(){//启动线程
?? thread=new Thread(this);
?? 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];//Here 1024 can be adjusted, should be consistent with the following 1024
?????????? int numbytesread=0;
?????????? Line.start ();
?????????? while (thread! = null) {
?????????????? Numbytesread = Line.read (data, 0,128);//The size of the fetch data (1024) is directly related to the speed of transmission, generally smaller and faster,
?????????????? try {
???????????????? Captrueoutputstream.write (data, 0, numbytesread);//write network stream
?????????????? }
?????????????? catch (Exception ex) {
?????????????????? Break
?????????????? }
?????????? }
?????????? Line.stop ();
?????????? Line.close ();
?????????? line = null;
?????????? try {
?????????????? Captrueoutputstream.flush ();
?????????????? Captrueoutputstream.close ();
?????????? } catch (IOException ex) {
?????????????? Ex.printstacktrace ();
?????????? }
?????? }
????
}
Java Voice capture component