0

我正在尝试在 android(2.3.3) 上开发相机应用程序。使用 eclipse 和 android 模拟器(所以没有设备)。我的应用程序有两个功能:拍照和捕捉视频。一键切换模式。第一个工作正常,但视频有问题。

第一个错误是:“setOutputFormat 在无效状态下调用:4”。在尝试设置 MediaRecorder 的输出格式时。如果我忽略这一点(将其设为注释行),那么我会收到“媒体服务器死机,相机服务器死机”错误(错误 100)

我是一个新的 android 开发人员,所以我只是在使用本教程:http: //developer.android.com/guide/topics/media/camera.html

这些错误的原因可能是我试图在没有设备的情况下进行测试吗?

这是清单许可:

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>

这是发生这些错误的代码:

private boolean prepareVideoRecorder() {


    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.reset();

    // Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

   // Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);


    // Set output format and encoding
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

    // Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));


    // Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

    // Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getmHolder().getSurface());

    //  Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}

private void videoRecording() {
    if (isRecording) {
        // stop recording and release camera
        mMediaRecorder.stop();  // stop the recording
        releaseMediaRecorder(); // release the MediaRecorder object
        mCamera.lock();         // take camera access back from MediaRecorder

        // inform the user that recording has stopped
        captureButton.setText(R.string.capture);
        isRecording = false;
    } else {
        // initialize video camera
        if (prepareVideoRecorder()) {
            // Camera is available and unlocked, MediaRecorder is prepared,
            // now you can start recording
            mMediaRecorder.start();

            captureButton.setText(R.string.stop);
            isRecording = true;
        } else {
            // prepare didn't work, release the camera
            releaseMediaRecorder();
        }
    }
}

这是 logcat 输出:

06-21 16:27:24.034: E/MediaRecorder(329): setOutputFormat called in an invalid state: 4
06-21 16:27:24.034: D/AndroidRuntime(329): Shutting down VM
06-21 16:27:24.054: W/dalvikvm(329): threadid=1: thread exiting with uncaught exception     (group=0x40015560)
06-21 16:27:24.054: E/AndroidRuntime(329): FATAL EXCEPTION: main
06-21 16:27:24.054: E/AndroidRuntime(329): java.lang.IllegalStateException
06-21 16:27:24.054: E/AndroidRuntime(329):  at android.media.MediaRecorder.setOutputFormat(Native Method)
06-21 16:27:24.054: E/AndroidRuntime(329):  at android.media.MediaRecorder.setProfile(MediaRecorder.java:286)
06-21 16:27:24.054: E/AndroidRuntime(329):  at com.bor.Fotograf.AndroidFotoActivity.prepareVideoRecorder(AndroidFotoActivity.java:221)
06-21 16:27:24.054: E/AndroidRuntime(329):  at com.bor.Fotograf.AndroidFotoActivity.videoRecording(AndroidFotoActivity.java:257)
06-21 16:27:24.054: E/AndroidRuntime(329):  at com.bor.Fotograf.AndroidFotoActivity.access$9(AndroidFotoActivity.java:245)
06-21 16:27:24.054: E/AndroidRuntime(329):  at com.bor.Fotograf.AndroidFotoActivity$6.onClick(AndroidFotoActivity.java:107)
06-21 16:27:24.054: E/AndroidRuntime(329):  at android.view.View.performClick(View.java:2485)
06-21 16:27:24.054: E/AndroidRuntime(329):  at android.view.View$PerformClick.run(View.java:9080)
06-21 16:27:24.054: E/AndroidRuntime(329):  at android.os.Handler.handleCallback(Handler.java:587)
06-21 16:27:24.054: E/AndroidRuntime(329):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 16:27:24.054: E/AndroidRuntime(329):  at android.os.Looper.loop(Looper.java:123)
06-21 16:27:24.054: E/AndroidRuntime(329):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-21 16:27:24.054: E/AndroidRuntime(329):  at java.lang.reflect.Method.invokeNative(Native Method)
06-21 16:27:24.054: E/AndroidRuntime(329):  at java.lang.reflect.Method.invoke(Method.java:507)
06-21 16:27:24.054: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-21 16:27:24.054: E/AndroidRuntime(329):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-21 16:27:24.054: E/AndroidRuntime(329):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

正如你所说,你需要一个设备来测试视频和麦克风功能,srry!

也许您可以尝试使用虚拟机并尝试安装它。

在这里你可以找到如何安装虚拟机;)

http://osxdaily.com/2012/02/23/android-4-ics-virtualbox/

于 2012-06-21T13:57:26.513 回答
0

没有android.permission.RECORD_VIDEO的权限!!!

您可能可以在这里找到答案setOutputFormat called in an invalid state: 4 (where and why)

于 2020-04-04T20:52:59.057 回答