无法启动前置摄像头的mediarecorder,它会抛出illegalStateException启动失败:-38

时间:2017-09-15 04:54:56

标签: android video camera

开始失败的意思是什么:-38?

我创建了两个CameraView(预览)对象和一个Camera Object。

使用处理程序我可以将相机切换一段时间,比如10秒打开后置摄像头和10秒打开前置摄像头。

现在我在录制前置摄像头方面遇到了一些问题。

当我点击录制按钮时,首先它会将相机记录10秒,然后将其保存到SD卡中。并在10秒后自动切换到前置摄像头进行录制。

后置摄像头录制工作正常。但前置摄像头预览显示正确,但没有录制视频,当我呼叫IllegalStateException start failed: -38

时,它显示mediarecorder.start();

这是代码

准备媒体

    mediaRecorder = new MediaRecorder();

    mCamera.unlock(); // lock camera for later use


    mediaRecorder.setCamera(mCamera); // lock camera for later use

    //mediaRecorder.setCamera(mCamera); // lock camera for later use
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    //mediaRecorder.setOrientationHint(90);
    mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));
    //mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
    //mediaRecorder.setVideoSize(320, 240);
    //mediaRecorder.setVideoFrameRate(15);

    //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    //mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    long l = System.currentTimeMillis();
    mediaRecorder.setOutputFile("/sdcard/" + l + ".mp4");
    mediaRecorder.setMaxDuration(600000); // Set max duration 60 sec.
    mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M

    try {
        mediaRecorder.prepare();
        Log.e(TAG, "prepareMediaRecorder: ");
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    Log.e(TAG, "prepareMediaRecorder: RETURN TRUE");

开始录制

if (!prepareMediaRecorder()) {
        Toast.makeText(getContext(), "Fail in prepareMediaRecorder()!\n - Ended -", Toast.LENGTH_LONG).show();
        //finish();
        return;
    }

    try {
        Log.e(TAG, "recordBackCameraVideo: START START ");
        mediaRecorder.start();
        Log.e(TAG, "recordBackCameraVideo: START END ");
    } catch (final Exception ex) {
        Log.i("---", "Exception in thread");
    }

    recording = true;

停止录制

try {
        Log.e(TAG, "stopRecording: STOP START");
        mediaRecorder.stop(); // stop the recording
        Log.e(TAG, "stopRecording: STOP END");
    } catch (RuntimeException stopException) {
        Log.e(TAG, "stopRecording: " + stopException.getMessage());
    }
    releaseMediaRecorder(); // release the MediaRecorder object
    Toast.makeText(getContext(), "Video captured!", Toast.LENGTH_LONG).show();
    recording = false;

1 个答案:

答案 0 :(得分:0)

试试这个。首先,您需要检查CamcorderProfile.hasProfile(" CameraID","传递您想要的质量")如果是真的那么您可以使用该质量进行视频录制,否则您需要使用其他一些CamcorderProfile品质。

下面的代码仅供我在项目中使用的参考,您可以根据需要更改代码。

private boolean prepareMediaRecorder() {
    mediaRecorder = new MediaRecorder();

    CamcorderProfile profile = null;
    if (!cameraFront) {
        profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    } else {
        if (CamcorderProfile.hasProfile(0, CamcorderProfile.QUALITY_HIGH)) {
            profile = CamcorderProfile.get(0, CamcorderProfile.QUALITY_HIGH);
        } else {
            profile = CamcorderProfile.get(0, CamcorderProfile.QUALITY_LOW);
        }
    }

    mCamera.unlock();
    if (!cameraFront) {
        // Back
        mediaRecorder.setOrientationHint(90);
    } else {
        // Front
        mediaRecorder.setOrientationHint(270);
    }
    mediaRecorder.setCamera(mCamera);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setOutputFormat(profile.fileFormat);
    mediaRecorder.setVideoEncoder(profile.videoCodec);
    mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
    mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
    mediaRecorder.setOutputFile(Utils.getOriginalFileName());
    mediaRecorder.setVideoSize(640, 480);
    mediaRecorder.setMaxDuration(50000); // Set max duration 5 sec.
    mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M

    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    return true;
}