Question

I'm trying to record audio and save to the sdcard. I'm getting an invalid state exception. I've added the correct permissions eg record_audio and write_external_storage. I've prepared the MediaRecorder before calling start().

Why am i getting this exception. Thanks in advance.

public void recordAudio() {

        audioFilePath = 
                 Environment.getExternalStorageDirectory().getAbsolutePath() + "/myaudio.3gp";

        try {
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mediaRecorder.setOutputFile(audioFilePath);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mediaRecorder.prepare();
          } catch (Exception e) {
           e.printStackTrace();
          }
        mediaRecorder.start();

        final ProgressDialog mProgressDialog = new ProgressDialog(RecordActivity.this);
        mProgressDialog.setTitle("Recording...");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setButton("Stop", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mProgressDialog.dismiss();
            mediaRecorder.stop();
            mediaRecorder.release();
            }
        });

        mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
            public void onCancel(DialogInterface p1) {
                mediaRecorder.stop();
                mediaRecorder.release();
            }
        });
        mediaRecorder.start();
        mProgressDialog.show();
    }


}//end of RecordActivity

.

10-31 14:48:50.034: E/MediaRecorder(29120): start called in an invalid state: 16
10-31 14:48:50.044: E/AndroidRuntime(29120): FATAL EXCEPTION: main
10-31 14:48:50.044: E/AndroidRuntime(29120): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carefreegroup.rr3/com.carefreegroup.rr3.RecordActivity}: java.lang.IllegalStateException
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.os.Looper.loop(Looper.java:158)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.app.ActivityThread.main(ActivityThread.java:5751)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at java.lang.reflect.Method.invokeNative(Native Method)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at java.lang.reflect.Method.invoke(Method.java:511)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at dalvik.system.NativeStart.main(Native Method)
10-31 14:48:50.044: E/AndroidRuntime(29120): Caused by: java.lang.IllegalStateException
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.media.MediaRecorder._start(Native Method)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.media.MediaRecorder.start(MediaRecorder.java:805)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at com.carefreegroup.rr3.RecordActivity.recordAudio(RecordActivity.java:65)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at com.carefreegroup.rr3.RecordActivity.onCreate(RecordActivity.java:25)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.app.Activity.performCreate(Activity.java:5165)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
10-31 14:48:50.044: E/AndroidRuntime(29120):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
Was it helpful?

Solution

You have two calls to mediaRecorder.start() {ref. Michael, above}. Also, after .stop()/.release(), add mediaRecorder=null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top