I try to open the front facing camera but Camera.open( camId ) returns null and throws the exception java.lang.RuntimeException: Fail to connect to camera service
.
I've looked around at similar problems and tried what I've found in those threads but it doesn't appear to be working for my program. I think it might have something to do with the fact that I try to run the camera as a service where I don't want any preview.
This is my start recording method:
Log.v(TAG, "Starting camera");
Intent intent = new Intent(getActivity(), RecorderService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startService(intent);
and this is my stop method:
Log.v(TAG, "Stopping camera");
getActivity().stopService(new Intent(getActivity(), RecorderService.class));
In the service's onStartCommand method i set all the settings according to Android documentation. I have made this exact code work in an app but when I try to "detach" it by implementing it in my Recorder.java class, I can't really seem to work around the case that I'm not opening it from an activity...
What I've tried:
The permissions in Android Manifest (camera, record_audio, record_video) and also uses-feature (camera and camera.front)
The camera is properly closed (stop(), reset(), stopPreview(), release())
Implement the service in a separate application. This works but as I want it to be "detached" from an activity I can't go into activities and start the camera from there. I'd prefer it to start from my Recorder.java which is supposed to be easily implemented into your application for user testing...
I suspect it might be the SurfaceHolder holding me back. Is it possible to make the camera record without dealing with SurfaceViews and SurfaceHolders in the activity?
Thanks for any help
More code (this is where the exception comes from):
private static Camera openFrontFacingCamera() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
Log.v(TAG, "Number of cameras: " + cameraCount);
for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
Camera.getCameraInfo( camIdx, cameraInfo );
if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ) {
try {
cam = Camera.open( camIdx );
} catch (Exception e){
// Camera is not available (in use or does not exist)
Log.d(TAG, "Camera did not open");
Log.d(TAG, e.toString());
e.printStackTrace();
}
}
}
return cam;
}