MediaRecorder 给出启动错误或 IllegalStateException

MediaRecorder gives start error or IllegalStateException

我正在使用 MediaRecorder 通过 android 的相机 API 录制视频。我遇到了一个非常奇怪的问题。

    private void startRecordingVideo() {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    File file = getAlbumDir();
    recorder.setOutputFile(file.getAbsolutePath());
    recorder.setMaxDuration(50000);
    recorder.setMaxFileSize(5000000);
    recorder.setPreviewDisplay(CameraBridgeViewBase.surfaceHolder.getSurface());
    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException | IOException e) {
        e.printStackTrace();
    }     
}

现在这给了我 MediaRecorder:启动失败:-19 错误。我检查了 this and 链接,上面写着删除 mediaRecorder.setVideoSize(sView.getWidth(), sView.getHeight()); 但我没有使用 setVideoSize(sView.getWidth(), sView.getHeight())。通过尝试和错误,我发现如果我删除编码器 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); 应用程序不会崩溃但会给出新的异常:

03-23 16:50:06.213 28226-28226/com.scenera.android.surveillance E/MediaRecorder: audio source is set, but audio encoder is not set

我不明白我在这里做错了什么。任何帮助将不胜感激。提前致谢。

问题是你没有设置摄像头,使用摄像头1 API你应该先打开摄像头,然后解锁并设置到录像机上。只有在那之后你才能继续 MediaRecorder 的配置(顺便说一句,这是 API 的一个非常漂亮的部分)

MediaRecorder recorder = new MediaRecorder();

Camera camera = Camera.open();
camera.unlock();
recorder.setCamera(camera);
recorder.setPreviewDisplay(surfaceHolder.getSurface());

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

File file = getAlbumDir();
recorder.setOutputFile(file.getAbsolutePath());

recorder.setMaxDuration(50000);
recorder.setMaxFileSize(5000000);
try {
    recorder.prepare();
    recorder.start();
} catch (IllegalStateException | IOException e) {
    e.printStackTrace();
}
  boolean mStartRecording=false;

首先在 activity 中创建这个变量 稍后尝试下面的代码

if( recorder == null ) {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
        File file = getAlbumDir();
        recorder.setOutputFile(file.getAbsolutePath());
        recorder.setMaxDuration(50000);
        recorder.setMaxFileSize(5000000);
        recorder.setPreviewDisplay(CameraBridgeViewBase.surfaceHolder.getSurface());
    }
    if(!mStartRecording) {
        try {
            recorder.prepare();
            recorder.start();
            mStartRecording = true;
        }  catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        mStartRecording = false;
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;
    }

试试这个,如果有效请告诉我。