簡體   English   中英

使用TargetDataLine來獲取實時音頻:緩沖區不變

[英]Using TargetDataLine to get real-time audio: buffer does not change

我有一些這樣的代碼:

  byte tempBuffer[] = new byte[10000];

  //call sleep thread (how long specified by second parameter)
  //After sleep time is up it sets stopCapture to true
  AudioSleepThread ast = new AudioSleepThread(this, seconds);
  ast.start();

  while(!this.stopCapture) {
    //this method blocks
    int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
    System.out.println(cnt);
    if (cnt>0) {
          // Subsequent requests must **only** contain the audio data.
          RequestThread reqt = new RequestThread(responseObserver, requestObserver, tempBuffer);
          reqt.start();
          //Add it to array list
          this.reqtArray.add(reqt);
    }
  }

我有一個tempBuffer,我一次存儲10000個字節。 每當我有10000個字節的音頻時,我都會通過請求線程將其發送以處理該音頻塊。 我的問題是,我不斷向每個請求線程中的每個發送相同緩沖區的音頻。

在我看來,應該發生的是targetDataLine一次讀取音頻10000個字節,並將包含音頻不同部分的每個tempBuffers傳遞給每個請求線程。

也許我誤解了TargetDataLine。

您只需在循環外創建一次tempBuffer 每次對targetDataLine.read調用targetDataLine.read用新數據覆蓋緩沖區的內容。 除非您在RequestThread構造函數中復制緩沖區,否則將導致問題。 您應該為每次讀取創建一個新的緩沖區:

while(!this.stopCapture) {
  byte tempBuffer[] = new byte[10000];
  //this method blocks
  int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);

您還必須注意讀取(您的cnt變量)返回的讀取字節數。 讀取並不能保證填充緩沖區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM