title: obtainBuffer timed out due to pcm_read() returned error n -5 on Galaxy S4
body: <p>I have an application that uses the AudioRecord API to capture audio on Android devices and it repeatedly fails on Galaxy S4 devices. This also occurs in other applications that try to record audio with both AudioRecord and MediaRecorder (AudioRec HQ for example). I was able to reproduce it in a test application using the code below:</p>
  
  <pre><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 &lt;&lt; 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 &gt; 0) {
                      fileOutputStream.write(buffer, 0, bytesRead);
                  }
              }
              while (bytesRead &gt; 0);
          } catch (Exception e) {
              Log.e("RecordingTestApp", e.toString());
          }
      }
  });
  mRecordThread.start();
  </code></pre>
  
  <p>These are the relevant logcat entries:</p>
  
  <pre><code>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-&gt;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-&gt;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
  </code></pre>
  
  <p>Here is the full logcat:
  <a href="http://pastebin.com/y3XQ1rMf" rel="nofollow">http://pastebin.com/y3XQ1rMf</a></p>
  
  <p>No exceptions are thrown when this happens, AudioRecord.read just blocks until the hardware has recovered and started recording again, but loses 2-4 seconds of audio data so it is very annoying for users that their audio files are missing large areas without any explanation as to why.</p>
  
  <p>Is this a known hardware issue or are there things I should be doing differently to record more reliably?
  Is there any way to detect that this issue has occurred</p>