GetBuffer timeout due to pcm_read () returned error n -5 on Galaxy S4

I have an application that uses the AudioRecord API to capture audio on Android devices, and it fails repeatedly on Galaxy S4 devices. This also happens in other applications that try to record audio with both AudioRecord and MediaRecorder (e.g. AudioRec HQ). I was able to play it in a test application using the following code:

final int bufferSize = AudioRecord.getMinBufferSize(8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, 8000, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize << 2);
mAudioRecord.startRecording();

mRecordThread = new Thread(new Runnable() {
    @Override
    public void run() {
        BufferedOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new BufferedOutputStream(new FileOutputStream(String.format(Locale.US, "/sdcard/%1$d.pcm", System.currentTimeMillis())));
            final byte[] buffer = new byte[bufferSize];
            int bytesRead;
            do {
                bytesRead = mAudioRecord.read(buffer, 0, buffer.length);
                if (bytesRead > 0) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                }
            }
            while (bytesRead > 0);
        } catch (Exception e) {
            Log.e("RecordingTestApp", e.toString());
        }
    }
});
mRecordThread.start();

These are the relevant logcat entries:

02-03 15:36:10.913: W/AudioRecord(20986): obtainBuffer timed out (is the CPU pegged?) user=001699a0, server=001699a0
02-03 15:36:11.394: E/alsa_pcm(208): Arec: error5
02-03 15:36:11.394: W/AudioStreamInALSA(208): pcm_read() returned error n -5, Recovering from error
02-03 15:36:11.424: D/ALSADevice(208): close: handle 0xb7730148 h 0x0
02-03 15:36:11.424: D/ALSADevice(208): open: handle 0xb7730148, format 0x2
02-03 15:36:11.424: D/ALSADevice(208): Device value returned is hw:0,0
02-03 15:36:11.424: V/ALSADevice(208): flags 11000000, devName hw:0,0
02-03 15:36:11.424: V/ALSADevice(208): pcm_open returned fd 39
02-03 15:36:11.424: D/ALSADevice(208): handle->format: 0x2
02-03 15:36:11.434: D/ALSADevice(208): setHardwareParams: reqBuffSize 320 channels 1 sampleRate 8000
02-03 15:36:11.434: W/AudioRecord(20986): obtainBuffer timed out (is the CPU pegged?) user=001699a0, server=001699a0
02-03 15:36:11.444: D/ALSADevice(208): setHardwareParams: buffer_size 640, period_size 320, period_cnt 2
02-03 15:36:20.933: W/AudioRecord(20986): obtainBuffer timed out (is the CPU pegged?) user=0017ade0, server=0017ade0
02-03 15:36:21.394: E/alsa_pcm(208): Arec: error5
02-03 15:36:21.394: W/AudioStreamInALSA(208): pcm_read() returned error n -5, Recovering from error
02-03 15:36:21.424: D/ALSADevice(208): close: handle 0xb7730148 h 0x0
02-03 15:36:21.424: D/ALSADevice(208): open: handle 0xb7730148, format 0x2
02-03 15:36:21.424: D/ALSADevice(208): Device value returned is hw:0,0
02-03 15:36:21.424: V/ALSADevice(208): flags 11000000, devName hw:0,0
02-03 15:36:21.424: V/ALSADevice(208): pcm_open returned fd 39
02-03 15:36:21.424: D/ALSADevice(208): handle->format: 0x2
02-03 15:36:21.434: D/ALSADevice(208): setHardwareParams: reqBuffSize 320 channels 1 sampleRate 8000
02-03 15:36:21.434: D/ALSADevice(208): setHardwareParams: buffer_size 640, period_size 320, period_cnt 2
02-03 15:36:21.454: W/AudioRecord(20986): obtainBuffer timed out (is the CPU pegged?) user=0017ade0, server=0017ade0

Here is the full logarithm: http://pastebin.com/y3XQ1rMf

, , AudioRecord.read , , 2-4 , , .

, -, ? ,

+3
2

, AudioRecord MediaRecorder pcm. , .

, OpenSL ES jni, , , Galaxy S4

0

, .

new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION, 8000,

AudioRecord 8000 . 44100. 8000 . , AudioRecord . :

mAudioRecord.getState()==STATE_INITIALIZED

getMinimumBufferSize. ERROR_BAD_VALUE, .

, , , . , , , , . : , , , . alsa drviers - , . , , startRecording , runnable.

,

mAudioRecord.getRecordingState()==RECORDING_RECORDING

, , .

, . , alsa -, , , , , , (, , Linux).

, , , , / .

, , VOICE_RECOGNITION . , DEFAULT. .

+1

All Articles