尝试为Media Recorder设置Encoder时发生IllegalStateException

时间:2015-03-26 08:39:05

标签: android video camera android-camera recording

目前我正在使用Custom Camera for Recording video

我已正确completed Take Picture from Camera feature

我也在尝试making Recording Video from Camera feature work

实际上,如果我没有尝试为视频设置Encoder,那就可以了。

但我想自定义录制视频功能,因此我已为Encoder设置Media Recorder并始终获得IllegalStateException

所以我想要的是:Can set encoder, both audio encoder and video encoder for video完全没有发生异常。

知道如何的人,

请告诉我,

真的,谢谢你,

以下是代码。

private boolean prepareVideoRecorder(int mode){
        // Should release before use new Preview for Recording Video mode
        CustomCamera.releaseCamera();

        // Initialize camera
        CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);

        // Set orientation display
        CustomCamera.setCameraDisplayOrientation(getActivity(), mode);

        // Should release before use new Preview for Recording Video mode
        CustomCamera.releaseMediaRecorder();

        CustomCamera.mMediaRecorder = new MediaRecorder();

        // Step 1: Unlock and set camera to MediaRecorder
        CustomCamera.mCamera.unlock();
        CustomCamera.mMediaRecorder.setCamera(CustomCamera.mCamera);

        // Step 2: Set sources
        CustomCamera.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        CustomCamera.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        // THESE LINES GET CRASH
        CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);

        // todo Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
        CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

        // Step 4: Set output file
        CustomCamera.mMediaRecorder.setOutputFile(getOutputMediaFile(MediaType.VIDEO).toString());

        // Step 5: Set the preview output
        CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());


        // Step 6: Prepare configured MediaRecorder
        try {
            CustomCamera.mMediaRecorder.prepare();

            CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        } catch (IllegalStateException e) {
            e.printStackTrace();
            CustomCamera.releaseMediaRecorder();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            CustomCamera.releaseMediaRecorder();
            return false;
        }
        return true;
    }

p / s:

虽然我设置了这些行

CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);

方法setProfile之前或之后(或setOutputFile)仍然会遇到此异常。

以下是thrown exception

    > I/MediaRecorderJNI﹕ prepare: surface=0x1cf9610 (identity=373)
    E/MediaRecorder﹕ audio encoder has already been set
 java.lang.IllegalStateException
    at android.media.MediaRecorder.setAudioEncoder(Native Method)
    at ui.fragment.custom.camera.CameraPreviewFragment.prepareVideoRecorder(CameraPreviewFragment.java:469)
    at ui.fragment.custom.camera.CameraPreviewFragment.onClick(CameraPreviewFragment.java:203)
    at android.view.View.performClick(View.java:3574)
    at android.view.View$PerformClick.run(View.java:14293)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4448)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
     at dalvik.system.NativeStart.main(Native Method)

setProfile()方法

之后设置以下代码
 CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);

并移除.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);例外中的行try-catch。它工作正常。

1 个答案:

答案 0 :(得分:1)

从try catch中删除CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);行,因为您尝试再次设置AudioEncoder,因此异常。

修改Encoder方法之后设置setProfile(),这是正确的。