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.