我正在尝试录制声音(使用Android的AudioRecord),同时播放录制的声音(使用Android的AudioTrack)。我按如下方式初始化录音机和输出音轨:
int buffersize = 2*AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, buffersize);
AudioTrack output = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
现在,当我按下开始按钮时,会调用此方法:
int buffersize = 5*AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
Thread recordThread = new Thread(new Runnable() {
public void run() {
recorder.startRecording();
output.play();
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() < startTime + 3000) { // Always record 3 seconds
byte[] myBuffer = new byte[buffersize];
recorder.read(myBuffer, 0, buffersize);
output.write(myBuffer, 0, buffersize);
}
recorder.stop();
recorder.release();
output.stop();
output.release();
stopThread(); // void stopThread() {recordThread = null;}
}
}
recordThread.start();
现在我所听到的只是噪音,就像我录制的音乐一样。 我究竟做错了什么?请帮忙。 提前致谢, Jeroen D。
编辑: 我发现了问题,即速率(8000)太低了。我在真正的手机上用44100试了一下(因为android模拟器只支持8000),现在质量很好。谢谢你的帮助。