0

I have bellow code for capturing video. At the first time this code works well but when I back from activity and choose capture video( I have a list at main which one of it choices is capture video) ,then gives me this error:java.lang.RuntimeException: Fail to connect to camera service error. I dont know what line of my code makes this error. How I fix this problem?

protected void startRecording() throws IOException 
{

 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)
 try{
     mrec.stop();

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

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

 mCamera = Camera.open();
 mCamera.lock();  
 surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
 surfaceHolder = surfaceView.getHolder();
 surfaceHolder.addCallback(this);
 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    Parameters params = mCamera.getParameters();
    mCamera.setParameters(params);
    mCamera.setDisplayOrientation(90);
    try {
        mCamera .setPreviewDisplay(surfaceHolder);
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mCamera .startPreview();

  }
     //-------------------------------------------------------------

    // release the recorder after recording
    private void releaseRecorder() {
            if( mrec!=null){
             Log.v("recorder release", "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("camera relese", "camera released");
                    mCamera.release();
                    mCamera=null;
            }
    }

      //-------------------------------------------------------------
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) 
{

}
 //-------------------------------------------------------------
@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) 
{

}
      //-------------------------------------------------------------
 public boolean onKeyDown(int keyCode, KeyEvent event) 
 {
      if (keyCode == KeyEvent.KEYCODE_BACK ) 
      {              
          if(isRecording)
        mCamera.stopPreview();
          releaseCamera();

          cheshmakLamp_Off();
          RecorderTimer_off();        
          clearTexts();

        VideoRecorderActivity.this.finish();
      }

      return super.onKeyDown(keyCode, event);
  }
      //--------------------------------------------------------------
 private void InitializeUI()
 {
     imgCheshmak=(ImageView)findViewById(R.id.imgcheshmak1);

        tvName=(TextView)findViewById(R.id.tvSetNameRecorder);
        tvTime=(TextView)findViewById(R.id.tvTimer1); 
        btnlistToggle=(Button) findViewById(R.id.btnToggleListVideo);

        mCamera = Camera.open();
        mCamera.lock();
        surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


        mToggleButton = (ToggleButton) findViewById(R.id.toggleRecordingButton);
        btnlistToggle.setEnabled(true);
 }
sarah
  • 125
  • 3
  • 12
  • You can see on LogCat which line cause the error. You can also post the LogCat, then we can try help you. – ramaral Jan 03 '14 at 18:36
  • see http://stackoverflow.com/questions/3527621/how-to-pause-and-resume-a-surfaceview-thread, I think your problem is `surfaceDestroyed`. – Shayan Pourvatan Jan 04 '14 at 04:56
  • @Shayanpourvatan please help me for this post.http://stackoverflow.com/questions/20968119/how-reslove-my-code-for-showing-visualizer-while-recording-error-is-audiofling and another ques: you told me in yahoo about mention u? what is mention? you are in what site every day? i didnt understand. – sarah Jan 07 '14 at 16:57
  • i can't see now, excuse me, i see that tomorrow, but i don't know the answer of your question, but i will searched for you. I'm in this site, and when you use @Name one notification send to me. – Shayan Pourvatan Jan 07 '14 at 17:10

0 Answers0