Wednesday, September 4, 2013

Android MediaRecorder.setMaxFileSize Exception due to file size

Android Media Recorder will stop functioning and show a Java.lang.RuntimeException if we provide a very small file size, for example if you will put 1024 as the MediaRecorder.setMaxFileSize parameter, the recording won't even start in the first place. Remember, the number provided as input to this method represents maximum number of bytes which would be stored in a file by the MediaRecorder.
Android MediaRecorder

The reason is that it takes almost 10 KB to store a audio recording of 1 second or so. When you will put 1024 bytes as a parameter, the MediaRecord will simple run out of space when it will try to stop the voice recording upon reaching the tiny number 1024 bytes. The error code returned by such a situation is -22, and the logging line will look something like the one given below:



E/AndroidRuntime(7284): java.lang.RuntimeException: stop failed.
E/MediaRecorder(6898): setParameters(max-filesize=1024) failed: -22








In second scenario, suppose we become generous and double the file size to 2048. The media recorder will start recording, but yet another Java.lang.RuntimeException will show up as soon as the maximum file size is reached and the MediaRecorder attempts to stop the recording. The exception trace will show

E/AndroidRuntime(7284): java.lang.RuntimeException: stop failed.
E/AndroidRuntime(7284): at android.media.MediaRecorder.stop(Native Method)

So my dear friends, keep your eyes open when specifying a MediaRecorder maximum recording size, make it at least 20 KB. You may write it like
mRecorder.setMaxFileSize(20480);
Or alternatively
mRecorder.setMaxFileSize(1024*20);

In order to specify a maximum file size of 250 MB,
mRecorder.setMaxFileSize(1024 * 1024 * 250);

Last but not least, I feel obliged to share with my readers my super secret implementation of OnInfoListener callback method, which will be called by Android system when maximum file size we specified earlier is reached

import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;

private MediaRecorder mRecordernew MediaRecorder();

mRecorder.setOnInfoListener(new OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
Log.e("LOGTAG", "onInfo " + what + " " + extra);

if(what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED){
Log.e("LOGTAG", "Max file size reached");
try {
 Thread.sleep(250);// just giving the system some time
} catch (InterruptedException e) {
 e.printStackTrace();
}
 stopRecording();// custom implementation
 startRecording(false);// custom implementation
}
}
});

Please feel free to talk back, share ideas, and ask questions.

3 comments:

  1. working fine on android OS 2.3 always i am getting this exception in android OS 4.2 java.lang.RuntimeException: stop failed at android.media.MediaRecorder.stop(Native Method)

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Hey, try this

      recorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
      @Override
      public void onInfo(MediaRecorder mr, int what, int extra) {
      if(what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED){

      //Log.e("LOGTAG", "Max file size reached");
      try {
      Thread.sleep(250);
      } catch (InterruptedException e) {
      e.printStackTrace();
      }

      if (recording) {
      recorder.stop();
      recording = false;
      finish();

      }
      }

      }
      });

      Delete

Feel free to talk back...