legalStateException异常的mediaRecor

legalStateException异常的mediaRecor

本文介绍了Android的视频录制 - IllegalStateException异常的mediaRecorder.start的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的 HTTPS教程://开发商。 android.com/guide/topics/media/camera.html#capture-video

因此​​我尝试启动相机遵循以下顺序:

As such I follow the below order when trying to start the camera:

  1. Camera.open
  2. camera.unlock
  3. mediaRecorder.setCamera
  4. mediaRecorder.setAudioSource
  5. mediaRecorder.setVideoSource
  6. mediaRecorder.setProfile
  7. mediaRecorder.setOutputFile
  8. mediaRecorder。prepare
  9. mediaRecorder.start< - 这是我得到的IllegalStateException异常

我可以找出什么可以去错了,因为我下面的指导,运行5.0.2

I can figure out what could be going wrong since I'm following the guide, running 5.0.2

private Camera mCamera;
private MediaRecorder mMediaRecorder;

public CameraActivity() {
    mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();
}

public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open();
    }
    catch (Exception e) { ... }
    return c;
}

public void startRecording() {

    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

    try {
        mMediaRecorder.prepare();
    }
    catch (IOException e) { ... }
    catch (IllegalStateException e) { ... }

    try {
        mMediaRecorder.start();
    }
    catch (Exception e) {
        Log.d(TAG, "exception on mediaRecorder.start" + e.toString()); // This is the exception that gets thrown on .start
    }
}

我的清单包括所有必要的权限

My manifest includes all necessary permissions

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.microphone" />

我也试过手动设置的格式,而不是使用.setProfile,相同的结果。

I have also tried manually setting format instead of using .setProfile, same results

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

更新

该文件确实产生,尽管它无法播放,当然,所以我知道它的工作到这一点。以prepare调用不抛出异常,并出现开始之前。同样的异常抛出的start()

The file is indeed created, though it's unplayable of course, so I know it's working up to that point. The call to prepare does not throw an exception, and occurs before start. Same exception thrown on start()

推荐答案

比较agains我的code,看来你错过了两个电话:

Comparing agains my code, it seems you're missing two calls:

mediaRecorder.setVideoSize(int width, int height)
mediaRecorder.setPreviewDisplay(SurfaceHolder surfaceHolder)

后者是最有可能是导致飞机失事的原因由于Android需要一个有效的preVIEW表面开始录制。这样做的目的,以prevent隐蔽摄像头的应用程序。

The latter is most likely to be causing the crash as Android requires a valid preview surface to start recording. This is done so to prevent hidden cameras apps.

有几十个没有preVIEW表面与记录的问题,但这<一个href="http://stackoverflow.com/questions/2386025/taking-picture-from-camera-without-$p$pview">one似乎总结一下,你需要做的绕过这个限制。其基本思路是,以调整你的表面是1×1,并把它传递给你的 mediaRecorder 实例。请记住,这可能不是所有的设备,虽然在工作

There are dozens of questions related to recording without a preview surface, but this one seems to sum up what you need to do to bypass this restriction.The basic idea is to resize your surface to be 1x1 and pass it to your mediaRecorder instance. Keep in mind that this may not work in all devices though.

这篇关于Android的视频录制 - IllegalStateException异常的mediaRecorder.start的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 16:52