From 0696dbf386d0e9f2f91ab911520a45f3abd65b96 Mon Sep 17 00:00:00 2001 From: Rastislav Kostrab Date: Tue, 19 Jan 2016 18:30:53 +0900 Subject: Camera2Video: Several fixes https://github.com/googlesamples/android-Camera2Video/pull/19 bugfixes: recognition of camera sensor orientation, closing preview session properly, setting MediaRecorder and recording after pressing the button Change-Id: I6e7ec3fb1dc2069d39e049dced4b3c3781013d87 --- .../Application/src/main/AndroidManifest.xml | 1 + .../android/camera2video/Camera2VideoFragment.java | 133 +++++++++++++++------ media/Camera2Video/README.md | 2 +- 3 files changed, 100 insertions(+), 36 deletions(-) (limited to 'media') diff --git a/media/Camera2Video/Application/src/main/AndroidManifest.xml b/media/Camera2Video/Application/src/main/AndroidManifest.xml index 5cb54286..67a90d06 100644 --- a/media/Camera2Video/Application/src/main/AndroidManifest.xml +++ b/media/Camera2Video/Application/src/main/AndroidManifest.xml @@ -23,6 +23,7 @@ + surfaces = new ArrayList(); + mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); Surface previewSurface = new Surface(texture); - surfaces.add(previewSurface); mPreviewBuilder.addTarget(previewSurface); - Surface recorderSurface = mMediaRecorder.getSurface(); - surfaces.add(recorderSurface); - mPreviewBuilder.addTarget(recorderSurface); - - mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() { + mCameraDevice.createCaptureSession(Arrays.asList(previewSurface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession cameraCaptureSession) { @@ -510,8 +515,6 @@ public class Camera2VideoFragment extends Fragment }, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); } } @@ -575,33 +578,91 @@ public class Camera2VideoFragment extends Fragment mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); - mMediaRecorder.setOutputFile(getVideoFile(activity).getAbsolutePath()); + if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) { + mNextVideoAbsolutePath = getVideoFilePath(getActivity()); + } + mMediaRecorder.setOutputFile(mNextVideoAbsolutePath); mMediaRecorder.setVideoEncodingBitRate(10000000); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight()); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); - int orientation = ORIENTATIONS.get(rotation); - mMediaRecorder.setOrientationHint(orientation); + switch (mSensorOrientation) { + case SENSOR_ORIENTATION_DEFAULT_DEGREES: + mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation)); + break; + case SENSOR_ORIENTATION_INVERSE_DEGREES: + mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation)); + break; + } mMediaRecorder.prepare(); } - private File getVideoFile(Context context) { - return new File(context.getExternalFilesDir(null), "video.mp4"); + private String getVideoFilePath(Context context) { + return context.getExternalFilesDir(null).getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp4"; } private void startRecordingVideo() { + if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) { + return; + } try { - // UI - mButtonVideo.setText(R.string.stop); - mIsRecordingVideo = true; + closePreviewSession(); + setUpMediaRecorder(); + SurfaceTexture texture = mTextureView.getSurfaceTexture(); + assert texture != null; + texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); + mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); + List surfaces = new ArrayList(); + + Surface previewSurface = new Surface(texture); + surfaces.add(previewSurface); + mPreviewBuilder.addTarget(previewSurface); + + mRecorderSurface = mMediaRecorder.getSurface(); + surfaces.add(mRecorderSurface); + mPreviewBuilder.addTarget(mRecorderSurface); + mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() { - // Start recording - mMediaRecorder.start(); - } catch (IllegalStateException e) { + @Override + public void onConfigured(CameraCaptureSession cameraCaptureSession) { + mPreviewSession = cameraCaptureSession; + updatePreview(); + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + // UI + mButtonVideo.setText(R.string.stop); + mIsRecordingVideo = true; + + // Start recording + mMediaRecorder.start(); + } + }); + } + + @Override + public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { + Activity activity = getActivity(); + if (null != activity) { + Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show(); + } + } + }, mBackgroundHandler); + } catch (CameraAccessException e) { + e.printStackTrace(); + } catch (IOException e) { e.printStackTrace(); } + + } + + private void closePreviewSession() { + if(mPreviewSession != null) { + mPreviewSession.close(); + mPreviewSession = null; + } } private void stopRecordingVideo() { @@ -611,11 +672,13 @@ public class Camera2VideoFragment extends Fragment // Stop recording mMediaRecorder.stop(); mMediaRecorder.reset(); + Activity activity = getActivity(); if (null != activity) { - Toast.makeText(activity, "Video saved: " + getVideoFile(activity), + Toast.makeText(activity, "Video saved: " + mNextVideoAbsolutePath, Toast.LENGTH_SHORT).show(); } + mNextVideoAbsolutePath = null; startPreview(); } @@ -687,4 +750,4 @@ public class Camera2VideoFragment extends Fragment } -} +} \ No newline at end of file diff --git a/media/Camera2Video/README.md b/media/Camera2Video/README.md index ac5084ca..e1f07bc9 100644 --- a/media/Camera2Video/README.md +++ b/media/Camera2Video/README.md @@ -44,7 +44,7 @@ Pre-requisites -------------- - Android SDK v23 -- Android Build Tools v23.0.0 +- Android Build Tools v23.0.2 - Android Support Repository Screenshots -- cgit v1.2.3