Search code examples
androidvideopreviewrecording

Android Video Preview and Video Recording


Hi guys i need to record a video for a particular duration which is working fine. But i need to add a 30 seconds of delay before starting the actual video recording but in the mean while i need to show the camera preview. I am wondering how to achieve it. By Code for camera object initialization is:

mCamera = Camera.open();
surfaceHolder = recorderView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

And My Recorder object initialization is as below:

File file = new File(path, filename);
mrec = new MediaRecorder();
mCamera.lock();
mCamera.unlock();
mrec.setCamera(mCamera);
mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoSize(320, 240);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile(path + filename);
mrec.prepare();

I am using a countdowntimer for the first 30 seconds and on its onfinsh implementation i am calling up a mrec.start() to start the actual recording and till that i am not getting the camera preview. Also there takes about one or two seconds delay before starting the recorder.


Solution

  • those two lines:

    mCamera.unlock();
    mrec.setCamera(mCamera); 
    

    stops the camera preview. You must postpone them (and all the setup after them) until the 30 seconds has passed.

    I don't like it as well, but it's how it works.