AudioRecord: buffer overflow?

I get a buffer overflow, and RECORDING with my application. Recording is done in Service . I could not understand why I was receiving this message from AudioFlinger .

Below I create an instance of the AudioRecord object and set its 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]; 

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: aRecorder.setPositionNotificationPeriod(framePeriod); - perhaps the period is too long for this bufferSiz e, and a faster (shorter) period will solve the problem.

Can someone tell me how to get rid of buffer overflows?

+6
source share
2 answers

To resolve this issue, change the size of the AudioRecord buffer to 2 times the minimum buffer size.

You can use the static method AudioRecord.getMinBufferSize() . This will give you the minimum buffer size that will be used for your current format.

The syntax of the getMinBufferSize () method is:

 public static int getMinBufferSize ( int sampleRateInHz, int channelConfig, int audioFormat) 

Anything less than this number will fail to create an AudioRecord object.

You had to reduce the size of the buffer so as not to overload the audio subsystem with data requirements.

Remember to put overridden methods ( @Override ) for the 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 reading the post: Android Sound Recording, Part 2

Another thing you could try is to use streams when writing and another process on the written bytes, thereby avoiding too much congestion on the main user interface thread.

Open source sample code for this approach: musicg_android_demo

Mark this post for more - android-audiorecord-class-process-live-mic-audio-quickly-set-up-callback-func

+8
source

It's because:

 framePeriod = bufferSize / bytesPerFrame; 

You need to multiply and not split buffering.

Try:

 framePeriod = bufferSize * bytesPerFrame; 

And if you need a sample: here is a complete audio capture class

hope this helps

+1
source

Source: https://habr.com/ru/post/949998/


All Articles