Java Voice capture component

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.