如果数据未被读取但 AudioRecord API 会丢失任何音频帧,但它会连续记录吗?

Can AudioRecord API loose any audio frames if the data from it is not getting read but it is continuously recording?

这个问题对我来说非常重要,因为我想让从 AudioRecord API 接收到的音频帧与绝对时间无关。所以,基本上问题是假设我已经调用了 AudioRecord.startRecording()。执行此操作后,我启动一个线程(将其称为 Thread1),该线程开始使用 AudioRecord.read(...)AudioRecorder 实例读取音频帧。虽然我的应用程序是 运行,但假设我的 Thread1 停滞了 500 毫秒。当 Thread1 恢复时,我会丢失一些音频数据还是 AudioRecord 维护一个缓冲区来处理这个(我有一个很大的推动它确实如此)?

  1. 如果是,AudioRecord 维护的缓冲区大小是多少?
  2. 它是根据设备 AudioRecord 的帧大小还是某个绝对持续时间定义的?
  3. 此外,从我调用 AudioRecord.startRecording() 到它实际开始记录数据,我预计会有多少延迟?

我知道我问了很多问题,如果有人能真正回答这些问题,我将不胜感激。

AudioRecord 用于在用户使用数据之前保存数据的唯一缓冲区是在初始化期间创建的缓冲区。您可以通过 bufferSizeInBytes 设置其大小。如果用户有一段时间不阅读 PCM - 数据将丢失并被新数据替换。因此,为确保您不会丢失它 - 指定相当大的缓冲区大小。计算很简单:

bufSz = samplingFreqHz * sampleSize * channelNum * bufCapacitySec;

例如要保存以 44100Hz 采样的 5 秒立体声 16 位 PCM,您需要 44100 * 2 * 2 * 5 = 882000 字节。因此,只需决定您的 reader 线程休眠多长时间,并为 AudioRecord 提供足够的缓冲区大小以在该休眠期间累积所有数据。

最重要的是

If yes, what is the size of the buffer that the AudioRecord maintains?

您有责任将适当的大小传递给构造函数调用。

Is it defined in terms of the frame size for the AudioRecord for the device or some absolute time duration?

只是字节数,需要自己计算合适的大小

Also, how much latency can I expect from the time I call AudioRecord.startRecording() till when it actually starts recording the data?

没有完美的答案。这取决于实际设备和 OS 版本。音频录制作为专用进程实现,您的命令和录制的数据通过 IPC,通常会有不可预测的延迟。 Android 中有一个关于音频延迟的 nice articles(它们主要与播放有关,但我想它也可能被插入到录音中)。