I'm getting buffer overflow while RECORDING
with my app. The recording is performed in a Service
. I could not figure out why I'm getting this message from AudioFlinger
.
Below I instantiate the AudioRecord
object and set it's callbacks.
bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
aRecorder = new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, bufferSize);
aRecorder.setRecordPositionUpdateListener(updateListener);
bytesPerSample = bitsPerSample / 8;
int bytesPerFrame = nChannels * bytesPerSample;
framePeriod = bufferSize / bytesPerFrame; // nr of frames that can be kept in a bufferSize dimension
int result = aRecorder.setPositionNotificationPeriod(framePeriod);
buffer = new byte[bufferSize];
The audioRecord callback:
private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpdateListener(){
public void onPeriodicNotification(AudioRecord recorder){
int result = aRecorder.read(buffer, 0, buffer.length);
}
public void onMarkerReached(AudioRecord recorder)
{}
};
I suspect the problem is related to the:aRecorder.setPositionNotificationPeriod(framePeriod);
- maybe the period is too big for this bufferSiz
e and a faster(smaller) period will solve the issue.
Could someone tells me how to get rid of the buffer overflow?
To fix that issue, change the buffer size of AudioRecord to 2 times the minimum buffer size.
You can use AudioRecord.getMinBufferSize()
static method. This will give you the minimum buffer size to use for your current format.
The syntax of getMinBufferSize() method is:
public static int getMinBufferSize (
int sampleRateInHz, int channelConfig, int audioFormat)
Anything less than this number will result in failure while creating the AudioRecord object.
You should have been reducing the buffer size, so as not to overwhelm the audio subsystem with demands for data.
Remember to put the overridden methods (@Override
) for audioRecord callback as follows:
private AudioRecord.OnRecordPositionUpdateListener updateListener = new AudioRecord.OnRecordPositionUpdateListener(){
@Override
public void onPeriodicNotification(AudioRecord recorder){
int result = aRecorder.read(buffer, 0, buffer.length);
}
@Override
public void onMarkerReached(AudioRecord recorder)
{}
};
I recommend to read the post: Android audio recording, part 2
One more thing that you could try is to use threads on recording and the other process on the recorded bytes, thus avoiding too much overload on the main UI thread.
The open source sample code for this approach: musicg_android_demo
Check this post for more - android-audiorecord-class-process-live-mic-audio-quickly-set-up-callback-func
Thats because :
framePeriod = bufferSize / bytesPerFrame;
You need to multiply and not divide your buffersize.
Try with :
framePeriod = bufferSize * bytesPerFrame;
And if you need a sample : here is a complete audio capture class
hope it helps