Ravi Bhandari
Ravi Bhandari

Reputation: 4698

Switching between camera not working in android

My Video Recoding app record video and it is working from back camera now i want to use front camera also to record video. So users can switch between camera. User can select by which camera he want to record the video. Initially when my activity loaded back camera is open and preview is start, but when i click on switch camera button then it gives exception that java.lang.RuntimeException: Fail to connect to camera service. Below is my code to switch camera :

private void initRecorder(Surface surface) throws IOException {

   try{
        if (mCamera != null) {
            mCamera.lock();
            mCamera.unlock();
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
            mHolder.addCallback(null);
            mHolder = null;
        }

        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        if(cameraToOpen==1){
            mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);

        } else if(cameraToOpen==2){
            mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
        }

        mCamera.lock();
        mCamera.setDisplayOrientation(90); 
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();
        mCamera.unlock();

    }catch(Exception e){

      }
 }

When user click on switch camera button then mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); gives exception. How to work with both cameras.

Edited:

When i open front camera when activity start then it is working fine. SO i can open both camera and record from both only when initial that camera is selected. But When i switch between camera then it gives exception unable to connect to camera service.

Upvotes: 1

Views: 1006

Answers (1)

Elio Lako
Elio Lako

Reputation: 1351

You can do the switch between cameras as stated below :

Button otherCamera = (Button) findViewById(R.id.OtherCamera);

otherCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inPreview) {
    camera.stopPreview();
}

//NB: if you don't release the current camera before switching, you app will crash
camera.release();

//swap the id of the camera to be used
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera = Camera.open(currentCameraId);

setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera);
try {
    camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
    e.printStackTrace();
}
camera.startPreview();
}

For recording video while switching the camera you can follow more here switching between cameras using mediarecorder.

Upvotes: 1

Related Questions