I'm trying to develop a video recorder with the front and back camera. The app can switch between the back (default) and front camera. When the app is activated the front camera, and I press the recording button, the app goes to on error method, and I release and init the camera and the recording, but the error persists another time. Any idea? UPDATE: I update the onError method, with the code below, and I think I don't make the release and the init camera well, because when the app executes this method the surface holder is black and doesn't give me what the camera sees
Here, my code:
onError method:
public void onError(MediaRecorder mr, int what, int extra) {
stopRecording();
//if Error 100, app must release the Camera object and instantiate a new one.
if (what == 100) {
if (error_100 == 2) { //Error 100 persists, change camera
if (Camera.getNumberOfCameras() <= 1) {
//No mas camaras para app
} else {
if (selected_camera == FRONT_CAMERA) {
selected_camera=BACK_CAMERA;
} else {
selected_camera=FRONT_CAMERA;
}
selected_camera_button.setEnabled(false);
Toast.makeText(this, "Initializing other camera", Toast.LENGTH_SHORT).show();
}
} else { //No camera init finish activity
if (error_100 == 3) {
Toast.makeText(this, "Initialize cameras failed", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(this, "Recording error has occurred. Stopping the recording", Toast.LENGTH_SHORT).show();
}
}
error_100++;
initCamera();
initCameraRecorder();
}
}
And the other method:
/** Initializes the back/front camera */
private boolean initCamera() {
try {
camera = getCameraInstance(selected_camera);
Camera.Parameters camParams = camera.getParameters();
camParams.set( "cam_mode", 1 );
camParams.set("orientation", "portrait");
checkCameraFlash(camParams);
//Establish the rotation angle camera
setAngleCameraRotation();
camera.setDisplayOrientation(angle_rotation_camera);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Establish 4:3 ratio and 480x640 if is posible
setAspectResolutionCamera(camParams);
camera.setParameters(camParams);
//Camera eye
video_view.getHolder().setFixedSize(height_video, width_video);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(height_video, width_video);
video_view.setLayoutParams(lp);
camera.lock();
surface_holder = video_view.getHolder();
surface_holder.addCallback(this);
surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setPreviewCamera();
} catch(Exception e) {
Log.v("RecordVideo", "Could not initialize the Camera");
return false;
}
return true;
}
/** Initializes the camera recorder before starts the recording */
private void initCameraRecorder() {
if(media_recorder != null) return;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".mp4";
File outFile = new File(output_file_name);
if(outFile.exists()) {
outFile.delete();
}
try {
camera.stopPreview();
camera.unlock();
media_recorder = new MediaRecorder();
media_recorder.setCamera(camera);
media_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
media_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
profile.videoBitRate = 885000; //Medium quality
profile.videoFrameHeight = height_video;
profile.videoFrameWidth = width_video;
media_recorder.setProfile(profile);
media_recorder.setMaxDuration(21000); // limit to 21 seconds
media_recorder.setOrientationHint(setOrientationCameraRecorder());
media_recorder.setPreviewDisplay(surface_holder.getSurface());
media_recorder.setOutputFile(output_file_name);
media_recorder.prepare();
Log.v("RecordVideo", "MediaRecorder initialized");
} catch(Exception e) {
Log.v("RecordVideo", "MediaRecorder failed to initialize");
e.printStackTrace();
}
}
/** Starts the recording video */
private void beginRecording() {
media_recorder.setOnInfoListener(this);
media_recorder.setOnErrorListener(this);
media_recorder.start();
record_button.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
}
/** Stops the recording video */
private void stopRecording() {
if (media_recorder != null) {
media_recorder.setOnErrorListener(null);
media_recorder.setOnInfoListener(null);
try {
media_recorder.stop();
} catch(IllegalStateException e) {
// This can happen if the recorder has already stopped.
Log.e("INFO:", "Got IllegalStateException in stopRecording");
}
releaseRecorder();
record_button.setTextColor(getResources().getColor(android.R.color.black));
releaseCamera();
}
video_task.cancel(true);
output_file_name.isEmpty();
}