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

android video camera

260 观看

1回复

454 作者的声誉

启动失败:-38是什么意思?

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

使用处理程序,我可以切换相机一段时间,例如打开后置摄像头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;
作者: Mohd Asif Ahmed 的来源 发布者: 2017 年 9 月 15 日

回应 1


0

2482 作者的声誉

决定

试试这个。首先,您需要检查CamcorderProfile.hasProfile(“ CameraID”,“通过您想要的质量”)。如果为true,则可以使用该质量进行视频录制,否则需要使用其他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;
}
作者: Andy Developer 发布者: 2017 年 9 月 15 日
32x32