MediaRecorder:java.lang.IllegalStateException,位于

时间:2018-10-08 08:40:08

标签: android-mediarecorder

当我启动设备并打开我的应用程序时,一切正常,但是在设备上使用另一个应用程序后,我的应用程序出现此错误。

public void startRecorder(){

    // Check if user has given permission to record audio
    int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);
        return;
    }
    if (mRecorder == null) {

        mRecorder = new MediaRecorder();
        mRecorder.setOutputFile("/dev/null");
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);


        try {
            mRecorder.prepare();
            mRecorder.start();
        } catch (java.io.IOException ioe) {
            mRecorder = null;
            android.util.Log.e("[Monkey]", "IOException: " + android.util.Log.getStackTraceString(ioe));

        } catch (java.lang.SecurityException e) {
            mRecorder = null;
            android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e));
        }

    }


}

10-08 10:32:42.260 18121-18121 / com.avatarmind.ipaldecibel E / ###:JNI_MOTIONSERVICE LOAD 10-08 10:32:42.260 18121-18121 / com.avatarmind.ipaldecibel E / ###:MotionService设置 10-08 10:32:42.526 18121-18121 / com.avatarmind.ipaldecibel E / MediaRecorder:启动失败:-38 10-08 10:32:42.529 18121-18121 / com.avatarmind.ipaldecibel E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.avatarmind.ipaldecibel,PID:18121     java.lang.RuntimeException:无法恢复活动{com.avatarmind.ipaldecibel / com.avatarmind.ipaldecibel.MainActivity}:java.lang.IllegalStateException         在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3173)         在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3204)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2548)         在android.app.ActivityThread.-wrap12(ActivityThread.java)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1406)         在android.os.Handler.dispatchMessage(Handler.java:102)         在android.os.Looper.loop(Looper.java:148)         在android.app.ActivityThread.main(ActivityThread.java:5509)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:772)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)      造成原因:java.lang.IllegalStateException         在android.media.MediaRecorder.start(本机方法)         在com.avatarmind.ipaldecibel.MainActivity.startRecorder(MainActivity.java:300)         在com.avatarmind.ipaldecibel.MainActivity.onResume(MainActivity.java:262)         在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)         在android.app.Activity.performResume(Activity.java:6342)         在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3162)         在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3204)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2548)         在android.app.ActivityThread.-wrap12(ActivityThread.java)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1406)         在android.os.Handler.dispatchMessage(Handler.java:102)         在android.os.Looper.loop(Looper.java:148)         在android.app.ActivityThread.main(ActivityThread.java:5509)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:772)         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)

2 个答案:

答案 0 :(得分:0)

似乎在恢复应用程序并尝试调用mRecorder.start()时,您处于非法状态。您的“活动”暂停时,您可能不释放MediaRecorder。

尝试下一个代码:

@Override
protected void onPause() {
    super.onPause();
    // if we are using MediaRecorder, release it first
    releaseMediaRecorder();
}

private void releaseMediaRecorder(){
    if (mMediaRecorder != null) {
        // clear recorder configuration
        mMediaRecorder.reset();
        // release the recorder object
        mMediaRecorder.release();
        mMediaRecorder = null;
    }
}

答案 1 :(得分:0)

尝试使用,对于 21-30 api 就可以了

setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)