2013-10-31 74 views
1

我試圖記錄音頻並保存到SD卡。我得到一個無效的狀態異常。我已經添加了正確的權限,例如record_audio和write_external_storage。我在調用start()之前準備好了MediaRecorder。MediaRecorder無效狀態16

爲什麼我得到這個異常。提前致謝。

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) 
+0

什麼是行號。 RecordActivity的25和65 –

+0

嘗試調試並確保您的錄像機已準備就緒。 –

+0

狀態16應該是'MEDIA_RECORDER_RECORDING',所以你可能試圖啓動一個已經啓動的MediaRecorder。 – Michael

回答

2

你有兩個呼叫mediaRecorder.start(){REF。邁克爾,上面}。另外,在.stop()/。release()之後,添加mediaRecorder = null。