我尝试使用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!!! ");
}
}