java 語音採集組件

來源:互聯網
上載者:User

標籤:output   bool   oat   getline   thread   read   資料   class   trace   

class Capture implements Runnable{

TargetDateLine line;//可以從中讀取音頻資料的某種類型的 DataLine

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];//此處的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();
?????????? }
?????? }
????
}

java 語音採集組件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.