2012-11-29 109 views
1

我試圖用AudioRecord類錄製一些音頻。這裏是我的代碼:AudioRecord - 空緩衝區

int audioSource = AudioSource.MIC; 
    int sampleRateInHz = 44100; 
    int channelConfig = AudioFormat.CHANNEL_IN_MONO; 
    int audioFormat = AudioFormat.ENCODING_PCM_16BIT; 
    int bufferSizeInShorts = 44100; 
    int bufferSizeInBytes = 2*bufferSizeInShorts; 

    short Data[] = new short[bufferSizeInShorts]; 
    Thread recordingThread; 

    AudioRecord audioRecorder = new AudioRecord(audioSource, 
               sampleRateInHz, 
               channelConfig, 
               audioFormat, 
               bufferSizeInBytes); 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
       getMenuInflater().inflate(R.menu.activity_main, menu); 
       return true; 
     } 

     public void startRecording(View arg0) { 
       audioRecorder.startRecording(); 
       recordingThread = new Thread(new Runnable() { 
        public void run() { 
         while (Data[bufferSizeInShorts-1] == 0) 
           audioRecorder.read(Data, 0, bufferSizeInShorts); 
        } 
       }); 
       audioRecorder.stop(); 
     } 

不幸的是我的短陣列在記錄結束後是空的。我可以請你幫我弄清楚有什麼問題嗎?

+0

你應該測試讀取的返回值來找出它。 (這就是返回值的原因,給你一些線索,發生了什麼)。此外,測試0的最後值可能無效,因爲0是有效值。 – njzk2

+0

你是對的測試最後的價值 - 我將不得不尋找不同的解決方案。但即使在測試中,我也應該寫一些寫到我的短陣列中的東西。 .read()函數返回0,這意味着沒有什麼複製到我的短緩衝區中:http://developer.android.com/reference/android/media/AudioRecord.html – Arxas

+0

是不是應該使用OnRecordPositionUpdateListener? – njzk2

回答

0

我想這個代碼可以幫助你更好地用所需的例子: - Link with Example

+0

我不確定它是如何幫助我的,因爲它是使用MediaPlayer類實現的MP3播放器。 – Arxas

+0

對不起,遲到的答覆。這是您可以解決您的問題的鏈接。我希望它可以幫助。鏈接如下: - [鏈接](https://sites.google.com/site/mojabdelhadi/technical -tutorials /機器人/機器人-音頻記錄教程) –