0

I am writing a video capture app for android. I have preview first time I want to capture. But after I press stop, I no longer get preview when I start recording again. How can I have preview option permanently?

protected void startRecording() throws IOException 
{
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) 
{
    throw new IOException("SD Card is not mounted.  It is " + state + ".");
}

// make sure the directory we plan to store the recording in exists
File directory = new File(this.Videopath).getParentFile();

if (!directory.exists() && !directory.mkdirs()) 
{
    throw new IOException("Path to file could not be created.");
}

 mCamera.stopPreview();
 mCamera.unlock();
 mrec = new MediaRecorder();  // Works well
 mrec.setCamera(mCamera);   
 mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
 mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
 mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
 mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
 mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
 mrec.setOutputFile(Videopath);
 mrec.setPreviewDisplay(surfaceHolder.getSurface());   
 mrec.prepare();
 isRecording=true;
 mrec.start();
}
//-------------------------------------------------------------
protected void stopRecording() 
{
    if(mrec !=null)
        mrec.stop();
    releaseMediaRecorder();
    isRecording=false;
}
//-------------------------------------------------------------
private void releaseMediaRecorder()
{
    if (mrec != null) 
    {
        mrec.reset();   // clear recorder configuration
        mrec.release(); // release the recorder object
        mrec = null;
        mCamera.lock();           // lock camera for later use
    }
}
//-------------------------------------------------------------
private void releaseCamera()
{
    if (mCamera != null)
    {
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}
//-------------------------------------------------------------
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) 
{
     try {
            mCamera .setPreviewDisplay(holder);
        } 
          catch (IOException e) 
          {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       mCamera .startPreview();  
}
 //-------------------------------------------------------------
@Override
public void surfaceCreated(SurfaceHolder holder) 
{
    if (mCamera != null)
       {
          Parameters params = mCamera.getParameters();
          mCamera.setParameters(params);
          mCamera.setDisplayOrientation(90);
          try {
            mCamera .setPreviewDisplay(holder);
        } 
          catch (IOException e) 
          {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          mCamera .startPreview();

       }
       else
       {
          Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show();
         VideoRecorderActivity.this.finish();
       }


}
 //-------------------------------------------------------------
@Override
public void surfaceDestroyed(SurfaceHolder holder) 
{


}
Adnan Mulla
  • 2,872
  • 3
  • 25
  • 35
setare shojaei
  • 145
  • 1
  • 12
  • I serched but couldnt catch any solving. – setare shojaei Dec 02 '13 at 15:03
  • 1
    Have you looked at http://stackoverflow.com/questions/7099032/problem-with-camera-when-orientation-changes ? Also, your complete LogCat error might help. – 2Dee Dec 02 '13 at 15:13
  • What's the API / version of Android running on your device? – NigelK Dec 02 '13 at 15:18
  • Prior to API level 14, you cannot call setDisplayOrientation when the preview is active. You start the preview in surfaceCreated but don't set previewing = true at that point (which I think you should). My guess is that you then enter surfaceChanged and as a result, don't stop the preview before calling the method. If making that change works, I'll post it as the answer. – NigelK Dec 02 '13 at 15:28

1 Answers1

0

After you press stop recording either you can play the recorded video or if you want to restart your preview then in your stoprecording method do this:

 protected void stopRecording() 
{
if(mrec !=null)
 try{
     mrec.stop();

    }catch(IllegalStateException e)
    {
   Log.e(TAG,"Got illegal ");

   }
 releaseRecorder();
 releaseCamera();
 isRecording=false;

 /*Here do the same process which you do while creating the preview i.e initilize the camera and create the preview */

  mCamera = Camera.open();
  camera.lock();    
  surfaceHolder = surfaceView.getHolder();
  surfaceHolder.addCallback(this); 
 try{
                mCamera .setPreviewDisplay(holder);
                mCamera .startPreview();
        }catch(IOException e)
        {
                Log.v(TAG,"could not start the preview ");
                e.printStackTrace();

        }            

}

// release the recorder after recording
    private void releaseRecorder() {
            if( mrec!=null){
             Log.v(TAG, "recorder released");
               mrec.release();
               mrec=null;
            }

    }
    // release the camera after recording
    private void releaseCamera() {
            if(mcamera!=null){
                    try{
                            mcamera.reconnect();

                    }catch(IOException e){
                            e.printStackTrace();
                    }
                    Log.v(TAG, "camera released");
                    mcamera.release();
                    mcamera=null;
            }
Smogger
  • 553
  • 5
  • 17
  • because when you stop recording,you are releasing the recorder as well as camera, so if you want to start the preview again you have to initilize the camera again and start the preview. – Smogger Dec 04 '13 at 08:46
  • I would recommend after when you press stop recording , play the recorded video and put one more button that will be used for retake i.e record again. – Smogger Dec 04 '13 at 08:48
  • but i am talking to you for first initializing this code in oncreate of activity.this dosnt makes any error after?mCamera = Camera.open(); surfaceView = (SurfaceView) findViewById(R.id.surface_camera); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); – setare shojaei Dec 04 '13 at 08:53
  • yes you can initilize this code in oncreate, what is the problem then? – Smogger Dec 04 '13 at 08:59
  • hi. please if possible see this link.it is for me and about capture video.http://stackoverflow.com/questions/20898733/how-fix-this-exception-on-android-fail-to-connect-to-camera-service# thanks. – setare shojaei Jan 03 '14 at 08:26
  • I am not able to understand where you are facing the problem , can you post some log cat error or clearly explain where it is showing error. – Smogger Jan 04 '14 at 08:55