1

我正在尝试录制音频并保存到 SD 卡。我得到一个无效的状态异常。我添加了正确的权限,例如 record_audio 和 write_external_storage。在调用 start() 之前,我已经准备好了 MediaRecorder。

为什么我得到这个异常。提前致谢。

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)
4

1 回答 1

2

你有两次调用 mediaRecorder.start() {ref. 迈克尔,以上}。此外,在 .stop()/.release() 之后,添加 mediaRecorder=null。

于 2014-07-09T05:18:47.050 回答