【发布时间】:2014-01-24 11:17:20
【问题描述】:
我正在尝试从 AudioRecord 录制 4 秒的集合,处理它们然后再次录制等等。我正在以 44100 个样本/秒的速度录制,您可以在下面的代码中看到。不得不提的是,我记录,或者至少我应该记录一组 19Khz 的脉冲。
int frequency = 44100;
int blockSize = 44100;
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration,
audioEncoding, blockSize * 8); // if I multiply blockSize by 4 it will only give 88200 buffer size
// start recording until explicitly stopped
while ( <stopCondition> ) {
recData = new ByteArrayOutputStream();
dos = new DataOutputStream(recData);
short[] buffer = new short[blockSize * 4]; // Save the raw PCM
// timePassed = false;
// timer.cancel();
// timer.start();
audioRecord.startRecording();
// while (!timePassed) {
int bufferReadResult = audioRecord.read(buffer, 0, blockSize * 4);
for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
try {
dos.writeShort(buffer[i]);
// buffer[i] = 0;
} catch (IOException e) {
e.printStackTrace();
}
}
// }
audioRecord.stop();
try {
dos.flush();
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
... process the recorded data
正如您从代码中看到的(已注释),我尝试使用 countDownTimer 在 4 秒后停止录制。它一次只能从 audioRecord 读取 1 秒(blockSize 没有乘以 4),但我不知道缓冲区是否被覆盖,因为偏移量为 0 - 我认为它确实如此,但我仍然不确定 :) 使用这种方法,它没有记录 4 秒内播放的脉冲数 - 播放了 16 个脉冲,只记录了 2-3 个。
我尝试不使用计时器并从 audioRecord blockSize * 4 = 44100 (=1s) * 4 = 4s 中读取,但在 Audacity 中我只看到 1 秒被记录 - 在代码中的某处,此处未显示,我写了记录数据到文件以检查输出。再次记录的脉冲数不正确,但这很明显,因为我只有 1 秒的记录数据。
有没有更好的方法来连续记录 X 秒的集合并对其进行处理?或者如果我的方法比我做错的还好?
谢谢。
【问题讨论】:
标签: java android audio-recording android-audiorecord