tags:

views:

466

answers:

1

I'm trying to build a simple video recorder for an Android device and I've had limited success and mixed results. It worked once, but now, without changing anything, I'm getting a "Camera is not available" error followed by "prepare failed -2147483648" error

The constructor for the previewer looks like

   private SurfaceHolder holder_;
   private MediaRecorder r_;
   public CameraPreview(Context context, AttributeSet attrs) {
       super(context, attrs);
       holder_ = getHolder();
       holder_.addCallback(this);
       holder_.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
       r_ = new MediaRecorder();
       r_.setAudioSource(MediaRecorder.AudioSource.MIC);
       r_.setVideoSource(MediaRecorder.VideoSource.CAMERA);
       r_.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
       r_.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
       r_.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    }

And my

surfaceCreated()

method looks like

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        r_.setPreviewDisplay(holder_.getSurface());
        r_.setOutputFile("/sdcard/test.mpg");
        if (r_ != null){
            try{
                r_.prepare();
                r_.start();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
     }

Any thoughts?

Thanks, Chris

+3  A: 

I have not used video recording, but with the Camera class for still pictures, you have to expressly release the Camera object via release(). Failing to do that means the camera will be tied up indefinitely (probably until a reboot).

If you have not called release() on the MediaRecorder, you may be encountering the same problem.

CommonsWare
That was my thinking as well and I made sure to include a r_.release() method in my onPause() method and all I've gotten from it is an InvalidStateException indicating that the camera isn't active. I'll try rebooting though, it's possible the first time it worked, I never released things and now I can't get a hold of them. Thanks!
Chris Thompson
That was it. The first time I ran the app, it never released the camera. Rebooting and altering the code to call r_.release() worked. Thanks!
Chris Thompson