MediaRecorder导致IllegalStateException

时间:2017-07-10 19:42:02

标签: android resources illegalstateexception

仅当用户按下后退按钮并重新输入意图时才会发生这种情况。我认为问题是我有一个文件启动MediaRecorder来获取声音,但是当用户退出时,MediaRecorder没有关闭,或者我有一个runOnUiThread,当按下后退按钮时,它可能没有关闭?

我有以下代码,我认为可行:

@Override
public void onBackPressed() {
    try {
        stop();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public void stop() throws IOException {
    if (mRecorder != null) {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }
}

我在runOnUiThread区块中的整个班级,在

private class CustomSense extends TimerTask {
    private String amps;

    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                amps = String.valueOf(sounder.getAmplitude());
                soundDisplay.setText(amps);
            }
        });
    }

    public String getAmps() {
        return amps;
    }
}

但也许我不太明白onBack是如何工作的还是runOnUiThread?我一直在浏览那些和MediaRecorder的文档,所以我很容易在那里丢失一些东西(注意,当进入另一个意图并重新开始时,只有当按下后退按钮并重新启动意图时才会发生这种情况)

我觉得问题是,当按下后退按钮并且它试图运行它们的两个实例导致IllegalStateException时我没有关闭,但我无法弄清楚当用户按下时如何实际关闭它们他们的手机后退按钮。

StackTrace如下:

FATAL EXCEPTION: main
                                                                 Process: PID: 28671
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{SensorDisplayActivity}: java.lang.IllegalStateException
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2671)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2736)
                                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6154)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                                  Caused by: java.lang.IllegalStateException
                                                                     at android.media.MediaRecorder.start(Native Method)
                                                                     at SoundDetect.start(SoundDetect.java:62)
                                                                     at SoundDetect.<init>(SoundDetect.java:82)
                                                                     at SensorDisplayActivity.onCreate(SensorDisplayActivity.java:89)
                                                                     at android.app.Activity.performCreate(Activity.java:6683)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2736) 
                                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:154) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6154) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

SoundDetect:62是

中的mRecorder.start()
 public void start() throws IOException {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/null.3gp";

            if (mRecorder == null) {
                mRecorder = new MediaRecorder();
                mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mRecorder.setOutputFile(mFileName);
                mRecorder.prepare();
                mRecorder.start();
            }
}

SoundDetect 82只是一个.start()调用

0 个答案:

没有答案