android video app video capture request
The android video app video capture request tutorial describes how to setup the video capture session request for recording & saving videos.
Get Code
The code to this android tutorial can be found on github
https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-capture
or you can run this command
git clone https://github.com/mobapptuts/android_camera2_api_video_app.git –branch camera2-video-capture
Steps
Create a method to setup the capture request session for recording
private void startRecord() { try { setupMediaRecorder(); SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); Surface previewSurface = new Surface(surfaceTexture); Surface recordSurface = mMediaRecorder.getSurface(); mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); mCaptureRequestBuilder.addTarget(previewSurface); mCaptureRequestBuilder.addTarget(recordSurface); mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { try { session.setRepeatingRequest( mCaptureRequestBuilder.build(), null, null ); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(CameraCaptureSession session) { } }, null); } catch (Exception e) { e.printStackTrace(); } }
Call the capture request method and then starting recording
private void checkWriteStoragePermission() { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { mIsRecording = true; mRecordImageButton.setImageResource(R.mipmap.btn_video_busy); try { createVideoFileName(); } catch (IOException e) { e.printStackTrace(); } startRecord(); mMediaRecorder.start(); } else { if(shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Toast.makeText(this, "app needs to be able to save videos", Toast.LENGTH_SHORT).show(); } requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT); } } else { mIsRecording = true; mRecordImageButton.setImageResource(R.mipmap.btn_video_busy); try { createVideoFileName(); } catch (IOException e) { e.printStackTrace(); } startRecord(); mMediaRecorder.start(); } }
Turn off the recording when the record button is pressed
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera2_video_image); createVideoFolder(); mMediaRecorder = new MediaRecorder(); mTextureView = (TextureView) findViewById(R.id.textureView); mRecordImageButton = (ImageButton) findViewById(R.id.videoOnlineImageButton); mRecordImageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mIsRecording) { mIsRecording = false; mRecordImageButton.setImageResource(R.mipmap.btn_video_online); mMediaRecorder.stop(); mMediaRecorder.reset(); startPreview(); } else { checkWriteStoragePermission(); } } }); }
Runtime permission work around for recording
When the application is first installed on android versions of marshmallow or later the application will be restarted. It’s onPause & onResume methods will be called after the external write storage permission has been granted.
This means the camera setup and connection will happen again. So when the camera is connected we will do the video recording setup.
Note this would normally only happen once. When the app is installed and first started.
In the camera device state callback setup for video recording if the application is currently in record mode.
private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(CameraDevice camera) { mCameraDevice = camera; if(mIsRecording) { try { createVideoFileName(); } catch (IOException e) { e.printStackTrace(); } startRecord(); mMediaRecorder.start(); } else { startPreview(); } // Toast.makeText(getApplicationContext(), // "Camera connection made!", Toast.LENGTH_SHORT).show(); }
Android video app video capture request summary
In the android video app video capture request tutorial we learned how to create a camera2 capture request session for video recording using the provide MediaRecorder object.