开发者

Video recording - unable to start camera preview

I'm working on a custom video recording class, and I'm having some issues getting the camera preview to display when the Activity first appears. I'm calling this function inside the surfaceCreated callback:

private void initRecorder(Surface surface) throws IOException {
// It is very important to unlock the camera before doing setCamera
// or it will results in a black preview
if(camera == null) {
    camera = Camera.open();
    camera.unlock();
}

if(recorder == null)
    recorder = new MediaRecorder();

recorder.setPreviewDisplay(surface);
recorder.setCamera(camera);

camera.startPreview();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile("/sdcard/test.mp4开发者_开发百科");
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
recorder.setVideoEncodingBitRate(15000000);
recorder.setMaxDuration(10000); // length of video in MS
recorder.setVideoSize(720, 480);
recorder.setVideoFrameRate(30);


try {
    recorder.prepare();
} catch (IllegalStateException e) {
    // This is thrown if the previous calls are not called with the 
    // proper order
    e.printStackTrace();
}
}

When the Activity starts, my app crashes saying:

java.lang.RuntimeException: startPreview failed

Above that error, I noticed a line saying:

attempt to use a locked camera from a different process (old pid 4894, new pid 6405)

When I step through the code, that error is occurring on the camera.startPreview() line. If I remove that line from my code, the preview shows up fine after I call recorder.start(), and prior to that I just have a black screen with my record button. Once I stop recording, the preview continues to show fine (I am calling camera.startPreview() after I stop recording).

Since I'm calling camera.unlock() only a few lines prior to starting the preview, and the two calls occur in the same function, how can I be having this error?

Edit: I tested the same code minus the call to startPreview() on a Droid X2 and a Droid 1, and it works fine. It looks like the EVO 4G is the problem. I will continue to investigate.


I answered a very similar question here: preview display in android media recorder

See if it helps you, it has a whole Activity that works with a preview and records a video.


camera.unlock() must be called from the same thread where camera was locked before. Check your logs for messages like Unlock call from pid 19322; currently locked to pid 17652.

Note that lock can be set by calling Camera.lock() or MediaRecorder.start()

Since API level 14, camera is automatically locked for applications in MediaRecorder.start(). Applications can use the camera (ex: zoom) after recording starts. There is no need to call this after recording starts or stops.


Arrange code like this and decrease video encoding rate. It is very high for your video size. This may not be creating a problem on your device because on some devices it is clipped internally.

private void initRecorder(Surface surface) throws IOException {
    // It is very important to unlock the camera before doing setCamera
    // or it will results in a black preview
    if (camera == null) {
        camera = Camera.open();
        camera.unlock();
    }

    if (recorder == null)
        recorder = new MediaRecorder();

    recorder.setCamera(camera);

    camera.startPreview();

    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    recorder.setVideoEncodingBitRate(2048000);
    recorder.setMaxDuration(10000); // length of video in MS
    recorder.setVideoSize(720, 480);
    recorder.setVideoFrameRate(30);
    recorder.setOutputFile("/sdcard/test.mp4");
    recorder.setPreviewDisplay(surface);
    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        // This is thrown if the previous calls are not called with the
        // proper order
        e.printStackTrace();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜