MediaRecorder崩溃?

时间:2018-03-22 18:50:14

标签: android opencv

我要录制视频,但它无效

public void startRecord() {
    if (preVideo()) {
        mRecorder.start();
    }
    isRecording = true;
}

public boolean preVideo() {
    if (mCamera == null) {
        return false;
    }

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);

    CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    mRecorder.setProfile(cpHigh);
    mRecorder.setOutputFile(createFile().toString());
    mRecorder.setVideoSize(mFrameWidth, mFrameHeight);

    return true;
}

和logcat;

  

致命的例外:主要                                                                      过程:pt.chambino.p.pulse,PID:6578                                                                      java.lang.IllegalStateException                                                                          在android.media.MediaRecorder.start(Native方法)                                                                          在org.opencv.android.MyJavaCameraView.startRecord(MyJavaCameraView.java:375)                                                                          at pt.chambino.p.pulse.App.onRecord(App.java:241)                                                                          at pt.chambino.p.pulse.App.onOptionsItemSelected(App.java:213)                                                                          在android.app.Activity.onMenuItemSelected(Activity.java:2600)                                                                          在com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1019)                                                                          在com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)                                                                          在com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)                                                                          在com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)                                                                          在com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)                                                                          在com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:119)                                                                          在android.view.View.performClick(View.java:4569)                                                                          在android.view.View $ PerformClick.run(View.java:18553)                                                                          在android.os.Handler.handleCallback(Handler.java:733)                                                                          在android.os.Handler.dispatchMessage(Handler.java:95)                                                                          在android.os.Looper.loop(Looper.java:212)                                                                          在android.app.ActivityThread.main(ActivityThread.java:5151)                                                                          at java.lang.reflect.Method.invokeNative(Native Method)                                                                          在java.lang.reflect.Method.invoke(Method.java:515)                                                                          在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)                                                                          在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)                                                                          在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式触发IllegalStateException MediaRecorder.start()

  • 您正在使用的录音输入已在使用中
  • 您尚未正确准备MediaRecorder进行录制。

the documentation

考虑这个状态图

enter image description here

打折第一个选项,因为没有足够的信息来确认或拒绝它,如果我要比较你的代码流程,看起来你错过了两个步骤:

  • setOutputFormat

  • prepare

您还将视频源设置为手机屏幕而非相机,因此我会将您的代码更改为:

public void startRecord() {
    try {
        if (preVideo()) {
            mRecorder.start();
            isRecording = true;
        }
    } catch(IOException | IllegalStateException e) {
        e.printStackTrace();
        // Error handling
    }
}

public boolean preVideo() throws IllegalStateException, IOException {
    if (mCamera == null) {
        return false;
    }

    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    mRecorder.setProfile(cpHigh);
    mRecorder.setOutputFile(createFile().toString());
    mRecorder.setVideoSize(mFrameWidth, mFrameHeight);

    mRecorder.prepare();

    return true;
}