MediaRecorder给出了启动错误或IllegalStateException

我正在使用MediaRecorder通过Android的Camera 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错误。 我检查了这一点,并说这个链接去除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

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


问题是你没有设置相机,使用Camera 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;

首先在活动中创建此变量,然后在代码后面尝试

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;
    }

试试这个,让我知道它的工作。

链接地址: http://www.djcxy.com/p/809.html

上一篇: MediaRecorder gives start error or IllegalStateException

下一篇: DevExtreme dxGrid lookup column form display id instead of DisplayExpr