问题描述:

This question is very important for me as I want to make my audio frames received from the AudioRecord API independent of the absolute time. So, basically the question is let's say I have called AudioRecord.startRecording(). After I do this, I start a thread (call it Thread1) that starts reading the audio frames from the AudioRecorder instance using AudioRecord.read(...). While my application is running, let's say my Thread1 gets stalled for 500 milliseconds. When the Thread1 resumes, would I loose some audio data or the AudioRecord maintains a buffer to handle this (which I have a big nudge that it does)?

  1. If yes, what is the size of the buffer that the AudioRecord maintains?
  2. Is it defined in terms of the frame size for the AudioRecord for the device or some absolute time duration?
  3. Also, how much latency can I expect from the time I call AudioRecord.startRecording() till when it actually starts recording the data?

I know that I have asked a lot of questions and I will be really grateful if someone could actually answer these.

网友答案:

The only buffer that AudioRecord uses to keep data before user consumes it is a buffer created during initialization. You can set its size via bufferSizeInBytes. If user doesn't read PCM for a while - data will be lost and replaced with new ones. So to ensure that you don't lose it - specify reasonably large buffer size. Calculation is pretty simple:

bufSz = samplingFreqHz * sampleSize * channelNum * bufCapacitySec;

E.g. to hold 5 sec of stereo 16-bit PCM sampled at 44100Hz you need 44100 * 2 * 2 * 5 = 882000 bytes. So just decide how long your reader thread sleeps and provide sufficient buffer size to AudioRecord to accumulate all data during that sleep.

To top it off

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

It is your responsibility to pass proper size to constructor call.

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

It is just number of bytes, you should calculate proper size by yourself

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

There is no perfect answer. It depends on actual device and OS version. Audio recording is implemented as dedicated process and your commands and recorded data go through IPC that in general has unpredictable delays. There is a nice articles about audio latency in Android (they are mostly about playback but I guess it may be interpolated to recording too).

相关阅读:
Top