AudioRecord: buffer overflow?

后端 未结 2 838
温柔的废话
温柔的废话 2021-02-03 13:47

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

2条回答
  •  不要未来只要你来
    2021-02-03 14:49

    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

提交回复
热议问题