我正在尝试使用 AudioRecord 将声音记录到一个数组(短或字节)中(MediaRecorder 就像一个魅力,但它只记录到文件,而不是数组)。但是,使用下面的代码我无法记录。当我尝试打印数组的内容时,我得到的只是零。我使用我的真实设备录制,而不是模拟器。
我将不胜感激任何建议。
public class SoundCapture extends Activity {
private int _buffer_size;
public final int SAMPLE_RATE = 8000;
private AudioRecord _recorder;
private short[] _wave;
public boolean captureSound() {
_buffer_size = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
_wave = new short[_buffer_size];
_recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, _buffer_size);
_recorder.startRecording();
//Recording for 5 seconds
synchronized(this) {
try{
this.wait(5000);
}
catch(InterruptedException e) {
e.printStackTrace();
return false;
}
}
_recorder.read(_wave, 0, SAMPLE_RATE);
_recorder.stop();
_recorder.release();
_recorder = null;
printWave();
return true;
}
private void printWave() {
System.out.println("Let the wave begin!!! ");
for(int i = 0; i < _wave.length; ++i) {
System.out.print(_wave[i] + ", ");
}
System.out.println("The End!!! ");
}
}