设置 mMediaRecorder.setProfile 时出现 IllegalStateException

Posted

技术标签:

【中文标题】设置 mMediaRecorder.setProfile 时出现 IllegalStateException【英文标题】:IllegalStateException when set the mMediaRecorder.setProfile 【发布时间】:2017-07-21 23:44:44 【问题描述】:

我尝试设置

mMediaRecorder.setProfile((CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA)));

当我启动相机服务时,我在日志中看到此错误:

    E/MediaRecorder: setOutputFormat called in an invalid state: 4
E/RecorderService: null ошибка
W/System.err: java.lang.IllegalStateException
W/System.err:     at android.media.MediaRecorder.setOutputFormat(Native Method)
W/System.err:     at android.media.MediaRecorder.setProfile(MediaRecorder.java:539)
W/System.err:     at com.hidecamera.hideng.services_record.RecorderService.startRecording(RecorderService.java:193)
W/System.err:     at com.hidecamera.hideng.services_record.RecorderService.onStartCommand(RecorderService.java:73)
W/System.err:     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4128)
W/System.err:     at android.app.ActivityThread.access$2400(ActivityThread.java:229)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1924)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7325)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

              [ 03-02 12:53:47.295 13476:13476 E/         ]
              Available MB : 2496291

如果我删除了视频录制开始但质量低的代码

 mServiceCamera.unlock();
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setCamera(mServiceCamera);
        mMediaRecorder.setAudiosource(MediaRecorder.AudioSource.CAMCORDER);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        selectFolder(MainActivity.isHideFolder);
        mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
        mMediaRecorder.setOrientationHint(CameraUtil.computePictureRotation());
        mMediaRecorder.setProfile((CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA)));
        mMediaRecorder.prepare();
        mMediaRecorder.start();

更新:

 public boolean startRecording() 
    try 
        Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();

        if (!MainActivity.currentCameraId) 
            mServiceCamera = Camera.open(0);//CAMERA_FACING_BACK
            Log.d(TAG, "CAMERA_FACING_BACK");
         else 
            mServiceCamera = Camera.open(1);//CAMERA_FACING_FRONT
            Log.d(TAG, "CAMERA_FACING_FRONT");
        


        CamcorderProfile profile = CamcorderProfile.get(getqualityvideo(MainActivity.quality));
        mServiceCamera.unlock();
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setCamera(mServiceCamera);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        int width = profile.videoFrameWidth;
        int height = profile.videoFrameHeight;
        mMediaRecorder.setVideoSize(width, height);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        selectFolder(MainActivity.isHideFolder);//папка с видео

        mMediaRecorder.setOrientationHint(CameraUtil.computePictureRotation());

        mMediaRecorder.prepare();
        mMediaRecorder.start();

        mRecordingStatus = true;

        return true;
     catch (IllegalStateException e) 
        Log.e(TAG, e.getMessage() + " ошибка IllegalStateException");

        return false;
     catch (IOException e) 
        Log.e(TAG, e.getMessage() + " ошибка IOException");

        return false;
     catch (NullPointerException e) 
        Log.e(TAG, e.getMessage() + " ошибка NullPointerException");

        return false;

     catch (Exception e) 

        Log.e(TAG, e.getMessage() + " ошибка Exception");

        return false;
    

在日志中开始记录后我看到了这个:

    03-04 00:03:33.073 23949-23949/com.joukehiden.camera E/MediaRecorder: start failed: -38
03-04 00:03:33.073 23949-23949/com.joukehiden.camera E/RecorderService: null ошибка IllegalStateException

                                                                        [ 03-04 00:03:33.076 23949:23949 E/         ]
                                                                        Available MB : 3052817

【问题讨论】:

【参考方案1】:

你会添加访问相机的权限吗?请检查一下。请参阅以下链接。 link link

【讨论】:

@upwardunkaind,对不起。我不认为你喜欢你所说的方式。有时,我们可能会错过一些小事。再次抱歉。【参考方案2】:

您需要删除以下内容,因为通过将配置文件设置为 mediarecorder 也会这样做

mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

删除下面两行也会做同样的事情

mMediaRecorder.setProfile((CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA)));

或者,如果没有使用 setProfile 设置媒体记录器参数,则保持以下顺序并尝试

CamcorderProfile profile =CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA);

mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoSize(640,480); /// video size make dynamic based on the requirement or view size
mMediaRecorder.setVideoFrameRate(profile.videoFrameRate); /// from profile
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

如果有任何事情请告诉我。

【讨论】:

以上是关于设置 mMediaRecorder.setProfile 时出现 IllegalStateException的主要内容,如果未能解决你的问题,请参考以下文章

短视频运营短视频剪辑 ⑤ ( 视频素材使用 | 设置插入后的视频素材属性 | 设置画面 | 设置音频 | 设置变速 | 设置动画 | 设置调节 )

SeeMusicMIDI 编辑功能 ( 速度设置 | SoundFont 音源设置 | 混响强度设置 | 混响时间设置 | 力度增益设置 | 实时 MIDI 设置 )

win10设置里的代理怎么设置

强制恢复出厂设置?

怎么设置标题样式?

HTMLHTML 注册表单案例 ① ( 表格设置 | 设置表格位置和大小 | 设置表格标题 | 表单设置 | 表格中设置单选按钮 )