0

Folks,

I am reading audio off the devices's microphone. Here is the pseudo code that is being run in a background thread:

while(true) {
   if (stopRecordingRequested()) {
       break;
   }
   int bytesRead = recorder.read(in, inSize);
   // do the processing
}

What I noticed is that method "read" returns data even if there is silence in the room.

I am thinking a tight loop such as the one above may simply peg the CPU even when the room is silent.

Is it possible to configure the audio recorder such that read() just waits until there is some noise?

Or, perhaps there is a different technique that doesn't peg the CPU.

Thank you in advance for your help.

4

1 回答 1

0

是否可以配置录音机,以便 read() 等到有一些噪音?

AudioRecord不可以。当声级低于某个点时,该课程还不够复杂,无法停止录音。

或者,也许有一种不同的技术不与 CPU 挂钩。

很简单,不要使用如此紧密的循环。请记住,如您所描述的那样紧密的循环将每隔几毫秒或几十毫秒执行一次,这样做似乎并不明智。

你有两个基本的选择...

  1. 当您设置音频格式、采样率和缓冲区大小时,请以您拥有的方式进行设置,例如足够的缓冲区用于 5 秒的录制。然后,您可以使用类似postDelayed调用 aHandler的方法每 4 或 4.5 秒读取一次缓冲区(以避免缓冲区溢出。
  2. 使用AudioRecord.OnRecordPositionUpdateListener回调并设置靠近缓冲区末尾的位置(但不是最后 - 再次避免溢出)并在每次调用侦听器时读取数据。
于 2013-10-17T22:20:36.833 回答