Hi, this is my code that records a video for 10 seconds. Remember that if you are taking a photo and also recording video, you have to initialise the camera when changing from video to photo and vice versa.
Preview preview = new Preview.Builder()
.setTargetResolution(
new Size(1280, 720))
.build();
cameraProvider.unbindAll();
CameraSelector cameraSelector =
new CameraSelector.Builder()
.requireLensFacing(CameraSelector.
LENS_FACING_BACK)
.build();
ImageAnalysis imageAnalysis =
new ImageAnalysis.Builder()
.setTargetResolution(
new Size(1280, 720))
.setBackpressureStrategy(ImageAnalysis.
STRATEGY_KEEP_ONLY_LATEST)
.build();
imageAnalysis.setAnalyzer(
cameraExecutor, image -> {
rotationDegrees = image.getImageInfo().getRotationDegrees();
});
videoCapture =
new VideoCaptureConfig.Builder()
.setTargetResolution(
new Size(480, 272))
.build();
cameraProvider.bindToLifecycle((LifecycleOwner)
context, cameraSelector, preview,
videoCapture);
}
private void takePhoto() {
imageFileName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File file = new File(file_path + "VID_" + imageFileName + ".mp4");
// CountDownTimer starts with 10 seconds and every onTick is 1 second
final int tenSec = 11 * 1000; // 1 minute in milli seconds
new CountDownTimer(tenSec, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
videoCapture.stopRecording();
}
}.start();
videoCapture.startRecording(file, cameraExecutor, new VideoCapture.OnVideoSavedCallback() {
@Override
public void onVideoSaved(@NonNull File file) {
System.out.println("****** Saving Video *****");
});
}
@Override
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
}
});
}
}