在模拟器上捕获Android视频

时间:2012-06-21 13:55:43

标签: java android eclipse

我正试图在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)

2 个答案:

答案 0 :(得分:0)

正如你所说,你需要有一个设备来测试de video和mic功能,srry!

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

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

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

答案 1 :(得分:0)

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

您也许可以在setOutputFormat called in an invalid state: 4 (where and why)

处找到答案