couple more safety checks

This commit is contained in:
tibbi 2016-06-16 22:07:29 +02:00
parent b97ca61f6c
commit cabc960317
2 changed files with 26 additions and 14 deletions

View File

@ -258,6 +258,12 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
return degrees; return degrees;
} }
private int getFinalRotation() {
int rotation = getMediaRotation(currCameraId);
rotation += compensateDeviceRotation();
return rotation % 360;
}
private void focusArea() { private void focusArea() {
if (camera == null) if (camera == null)
return; return;
@ -358,7 +364,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
private void cleanupRecorder() { private void cleanupRecorder() {
if (recorder != null) { if (recorder != null) {
if (isRecording) { if (isRecording) {
recorder.stop(); stopRecording();
} }
recorder.release(); recorder.release();
@ -448,6 +454,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
if (camera == null || recorder != null || !isSurfaceCreated) if (camera == null || recorder != null || !isSurfaceCreated)
return; return;
camera.lock();
final Camera.Size preferred = parameters.getPreferredPreviewSizeForVideo(); final Camera.Size preferred = parameters.getPreferredPreviewSizeForVideo();
parameters.setPreviewSize(preferred.width, preferred.height); parameters.setPreviewSize(preferred.width, preferred.height);
camera.setParameters(parameters); camera.setParameters(parameters);
@ -456,8 +463,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
isVideoMode = true; isVideoMode = true;
recorder = new MediaRecorder(); recorder = new MediaRecorder();
recorder.setCamera(camera); recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT); recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
curVideoPath = Utils.getOutputMediaFile(getContext(), false); curVideoPath = Utils.getOutputMediaFile(getContext(), false);
if (curVideoPath.isEmpty()) { if (curVideoPath.isEmpty()) {
@ -465,17 +472,15 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
return; return;
} }
final Camera.Size videoSize = getOptimalVideoSize();
final CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); final CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
cpHigh.videoFrameWidth = videoSize.width;
cpHigh.videoFrameHeight = videoSize.height;
recorder.setProfile(cpHigh); recorder.setProfile(cpHigh);
recorder.setOutputFile(curVideoPath); recorder.setOutputFile(curVideoPath);
recorder.setPreviewDisplay(surfaceHolder.getSurface()); recorder.setPreviewDisplay(surfaceHolder.getSurface());
final Camera.Size videoSize = getOptimalVideoSize(); int rotation = getFinalRotation();
recorder.setVideoSize(videoSize.width, videoSize.height);
int rotation = getMediaRotation(currCameraId);
rotation += compensateDeviceRotation();
rotation %= 360;
initVideoRotation = rotation; initVideoRotation = rotation;
recorder.setOrientationHint(rotation); recorder.setOrientationHint(rotation);
@ -484,6 +489,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
} catch (Exception e) { } catch (Exception e) {
Utils.showToast(getContext(), R.string.video_setup_error); Utils.showToast(getContext(), R.string.video_setup_error);
Log.e(TAG, "initRecorder " + e.getMessage()); Log.e(TAG, "initRecorder " + e.getMessage());
releaseCamera();
} }
} }
@ -498,14 +504,19 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
} }
private void startRecording() { private void startRecording() {
camera.lock(); if (initVideoRotation != getFinalRotation()) {
camera.unlock(); cleanupRecorder();
initRecorder();
}
try { try {
camera.unlock();
recorder.start(); recorder.start();
isRecording = true; isRecording = true;
} catch (Exception e) { } catch (Exception e) {
Utils.showToast(getContext(), R.string.video_setup_error); Utils.showToast(getContext(), R.string.video_setup_error);
Log.e(TAG, "toggleRecording " + e.getMessage()); Log.e(TAG, "toggleRecording " + e.getMessage());
releaseCamera();
} }
} }
@ -518,8 +529,10 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
new File(curVideoPath).delete(); new File(curVideoPath).delete();
Utils.showToast(getContext(), R.string.video_saving_error); Utils.showToast(getContext(), R.string.video_saving_error);
Log.e(TAG, "stopRecording " + e.getMessage()); Log.e(TAG, "stopRecording " + e.getMessage());
releaseCamera();
} finally { } finally {
recorder = null; recorder = null;
isRecording = false;
} }
} }
@ -527,7 +540,6 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
if (file.exists() && file.length() == 0) { if (file.exists() && file.length() == 0) {
file.delete(); file.delete();
} }
isRecording = false;
} }
@Override @Override

View File

@ -56,11 +56,11 @@ public class Utils {
} }
} }
final String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
if (isPhoto) { if (isPhoto) {
return mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"; return mediaStorageDir.getPath() + File.separator + "IMG_" + timestamp + ".jpg";
} else { } else {
return mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4"; return mediaStorageDir.getPath() + File.separator + "VID_" + timestamp + ".mp4";
} }
} }