<code> private void startCamera() {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
// Used to bind the lifecycle of cameras to the lifecycle owner
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
// Preview
Preview preview = new Preview.Builder()
.build();
preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
Recorder recorder = new Recorder.Builder()
.setQualitySelector(
QualitySelector.from(
Quality.LOWEST,
FallbackStrategy.lowerQualityOrHigherThan(Quality.LOWEST)
)
)
.build();
videoCapture = VideoCapture.withOutput(recorder);
// Select front camera as default
CameraSelector cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA;
try {
// // Unbind use cases before rebinding
cameraProvider.unbindAll();
//
// Bind use cases to camera
// cameraProvider.bindToLifecycle(this, cameraSelector, preview, videoCapture);
LifecycleCameraController cameraController = new LifecycleCameraController(this);
cameraController.bindToLifecycle(this);
cameraController.setCameraSelector(CameraSelector.DEFAULT_FRONT_CAMERA);
viewFinder.setController(cameraController);
} catch (Exception e) {
Log.e(TAG, "Use case binding failed"+e);
// throw new RuntimeException(e);
}
} catch (ExecutionException | InterruptedException exc) {
Log.e("RecordingActivity", "Use case binding failed", exc);
}
}, ContextCompat.getMainExecutor(this));
}
private void captureVideo() {
VideoCapture<Recorder> videoCapture = this.videoCapture;
if (videoCapture == null) return;
iconChange.setEnabled(false);
Recording curRecording = recording;
if (curRecording != null) {
// Stop the current recording session.
capturStart = false;
playStopRecordingSound();
curRecording.stop();
recording = null;
return;
}
// Generate the file name
String name = position != null ? position.replace(".", "") + ".mp4" : "default_video.mp4";
File file = new File(LocalStoragManageService.VIDEOS_PATH + File.separator + name);
if (file.exists()) {
// Delete the old file
file.delete();
}
// Create a new file with the same name
File newFile = new File(LocalStoragManageService.VIDEOS_PATH + File.separator + name);
savedPath = newFile.getAbsolutePath();
FileOutputOptions fileOutputOptions = new FileOutputOptions.Builder(newFile).build();
PendingRecording pendingRecording = videoCapture.getOutput().prepareRecording(this, fileOutputOptions);
if (PermissionChecker.checkSelfPermission(
this,
Manifest.permission.RECORD_AUDIO
) == PermissionChecker.PERMISSION_GRANTED) {
pendingRecording.withAudioEnabled();
}
recording = pendingRecording.start(ContextCompat.getMainExecutor(this), captureListener);
}
private final Consumer<VideoRecordEvent> captureListener = new Consumer<VideoRecordEvent>() {
@Override
public void accept(VideoRecordEvent recordEvent) {
updateUI(recordEvent);
if (recordEvent instanceof VideoRecordEvent.Start) {
iconChange.setImageResource(R.drawable.ellipce_stop);
iconChange.setEnabled(true);
} else if (recordEvent instanceof VideoRecordEvent.Finalize) {
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) recordEvent;
if (!finalizeEvent.hasError()) {
String msg = getResources().getString(R.string.video_capture_success);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
Log.d(TAG, msg);
// Handle the async task with ExecutorService
executorService.submit(() -> {
Uri outputUri = finalizeEvent.getOutputResults().getOutputUri();
String path = savedPath;
Intent intent = new Intent(CameraVideo.this, VideoDisplay.class);
intent.putExtra("position", position);
intent.putExtra("subPosition", subPosition);
intent.putExtra("path", path);
startActivity(intent);
finish();
});
} else {
if (recording != null) {
recording.close();
recording = null;
}
Log.e(TAG, "Video capture ends with error: " + finalizeEvent.getError());
}
iconChange.setImageResource(R.drawable.ellipse);
iconChange.setEnabled(true);
}
}
};
</code>
<code> private void startCamera() {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
// Used to bind the lifecycle of cameras to the lifecycle owner
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
// Preview
Preview preview = new Preview.Builder()
.build();
preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
Recorder recorder = new Recorder.Builder()
.setQualitySelector(
QualitySelector.from(
Quality.LOWEST,
FallbackStrategy.lowerQualityOrHigherThan(Quality.LOWEST)
)
)
.build();
videoCapture = VideoCapture.withOutput(recorder);
// Select front camera as default
CameraSelector cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA;
try {
// // Unbind use cases before rebinding
cameraProvider.unbindAll();
//
// Bind use cases to camera
// cameraProvider.bindToLifecycle(this, cameraSelector, preview, videoCapture);
LifecycleCameraController cameraController = new LifecycleCameraController(this);
cameraController.bindToLifecycle(this);
cameraController.setCameraSelector(CameraSelector.DEFAULT_FRONT_CAMERA);
viewFinder.setController(cameraController);
} catch (Exception e) {
Log.e(TAG, "Use case binding failed"+e);
// throw new RuntimeException(e);
}
} catch (ExecutionException | InterruptedException exc) {
Log.e("RecordingActivity", "Use case binding failed", exc);
}
}, ContextCompat.getMainExecutor(this));
}
private void captureVideo() {
VideoCapture<Recorder> videoCapture = this.videoCapture;
if (videoCapture == null) return;
iconChange.setEnabled(false);
Recording curRecording = recording;
if (curRecording != null) {
// Stop the current recording session.
capturStart = false;
playStopRecordingSound();
curRecording.stop();
recording = null;
return;
}
// Generate the file name
String name = position != null ? position.replace(".", "") + ".mp4" : "default_video.mp4";
File file = new File(LocalStoragManageService.VIDEOS_PATH + File.separator + name);
if (file.exists()) {
// Delete the old file
file.delete();
}
// Create a new file with the same name
File newFile = new File(LocalStoragManageService.VIDEOS_PATH + File.separator + name);
savedPath = newFile.getAbsolutePath();
FileOutputOptions fileOutputOptions = new FileOutputOptions.Builder(newFile).build();
PendingRecording pendingRecording = videoCapture.getOutput().prepareRecording(this, fileOutputOptions);
if (PermissionChecker.checkSelfPermission(
this,
Manifest.permission.RECORD_AUDIO
) == PermissionChecker.PERMISSION_GRANTED) {
pendingRecording.withAudioEnabled();
}
recording = pendingRecording.start(ContextCompat.getMainExecutor(this), captureListener);
}
private final Consumer<VideoRecordEvent> captureListener = new Consumer<VideoRecordEvent>() {
@Override
public void accept(VideoRecordEvent recordEvent) {
updateUI(recordEvent);
if (recordEvent instanceof VideoRecordEvent.Start) {
iconChange.setImageResource(R.drawable.ellipce_stop);
iconChange.setEnabled(true);
} else if (recordEvent instanceof VideoRecordEvent.Finalize) {
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) recordEvent;
if (!finalizeEvent.hasError()) {
String msg = getResources().getString(R.string.video_capture_success);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
Log.d(TAG, msg);
// Handle the async task with ExecutorService
executorService.submit(() -> {
Uri outputUri = finalizeEvent.getOutputResults().getOutputUri();
String path = savedPath;
Intent intent = new Intent(CameraVideo.this, VideoDisplay.class);
intent.putExtra("position", position);
intent.putExtra("subPosition", subPosition);
intent.putExtra("path", path);
startActivity(intent);
finish();
});
} else {
if (recording != null) {
recording.close();
recording = null;
}
Log.e(TAG, "Video capture ends with error: " + finalizeEvent.getError());
}
iconChange.setImageResource(R.drawable.ellipse);
iconChange.setEnabled(true);
}
}
};
</code>
private void startCamera() {
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -> {
try {
// Used to bind the lifecycle of cameras to the lifecycle owner
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
// Preview
Preview preview = new Preview.Builder()
.build();
preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
Recorder recorder = new Recorder.Builder()
.setQualitySelector(
QualitySelector.from(
Quality.LOWEST,
FallbackStrategy.lowerQualityOrHigherThan(Quality.LOWEST)
)
)
.build();
videoCapture = VideoCapture.withOutput(recorder);
// Select front camera as default
CameraSelector cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA;
try {
// // Unbind use cases before rebinding
cameraProvider.unbindAll();
//
// Bind use cases to camera
// cameraProvider.bindToLifecycle(this, cameraSelector, preview, videoCapture);
LifecycleCameraController cameraController = new LifecycleCameraController(this);
cameraController.bindToLifecycle(this);
cameraController.setCameraSelector(CameraSelector.DEFAULT_FRONT_CAMERA);
viewFinder.setController(cameraController);
} catch (Exception e) {
Log.e(TAG, "Use case binding failed"+e);
// throw new RuntimeException(e);
}
} catch (ExecutionException | InterruptedException exc) {
Log.e("RecordingActivity", "Use case binding failed", exc);
}
}, ContextCompat.getMainExecutor(this));
}
private void captureVideo() {
VideoCapture<Recorder> videoCapture = this.videoCapture;
if (videoCapture == null) return;
iconChange.setEnabled(false);
Recording curRecording = recording;
if (curRecording != null) {
// Stop the current recording session.
capturStart = false;
playStopRecordingSound();
curRecording.stop();
recording = null;
return;
}
// Generate the file name
String name = position != null ? position.replace(".", "") + ".mp4" : "default_video.mp4";
File file = new File(LocalStoragManageService.VIDEOS_PATH + File.separator + name);
if (file.exists()) {
// Delete the old file
file.delete();
}
// Create a new file with the same name
File newFile = new File(LocalStoragManageService.VIDEOS_PATH + File.separator + name);
savedPath = newFile.getAbsolutePath();
FileOutputOptions fileOutputOptions = new FileOutputOptions.Builder(newFile).build();
PendingRecording pendingRecording = videoCapture.getOutput().prepareRecording(this, fileOutputOptions);
if (PermissionChecker.checkSelfPermission(
this,
Manifest.permission.RECORD_AUDIO
) == PermissionChecker.PERMISSION_GRANTED) {
pendingRecording.withAudioEnabled();
}
recording = pendingRecording.start(ContextCompat.getMainExecutor(this), captureListener);
}
private final Consumer<VideoRecordEvent> captureListener = new Consumer<VideoRecordEvent>() {
@Override
public void accept(VideoRecordEvent recordEvent) {
updateUI(recordEvent);
if (recordEvent instanceof VideoRecordEvent.Start) {
iconChange.setImageResource(R.drawable.ellipce_stop);
iconChange.setEnabled(true);
} else if (recordEvent instanceof VideoRecordEvent.Finalize) {
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) recordEvent;
if (!finalizeEvent.hasError()) {
String msg = getResources().getString(R.string.video_capture_success);
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
Log.d(TAG, msg);
// Handle the async task with ExecutorService
executorService.submit(() -> {
Uri outputUri = finalizeEvent.getOutputResults().getOutputUri();
String path = savedPath;
Intent intent = new Intent(CameraVideo.this, VideoDisplay.class);
intent.putExtra("position", position);
intent.putExtra("subPosition", subPosition);
intent.putExtra("path", path);
startActivity(intent);
finish();
});
} else {
if (recording != null) {
recording.close();
recording = null;
}
Log.e(TAG, "Video capture ends with error: " + finalizeEvent.getError());
}
iconChange.setImageResource(R.drawable.ellipse);
iconChange.setEnabled(true);
}
}
};
I used above methods for starting the camera and video capture in android project with java. When I click on the capture button it suddenly close the video recording and return the error as “The video frame producer became inactive before any data was received.
“.
And also I used the version of these libraries as “1.1.0-beta02”. So I couldn’t use the
<code>cameraProvider.bindToLifecycle(this, cameraSelector, preview, videoCapture);
</code>
<code>cameraProvider.bindToLifecycle(this, cameraSelector, preview, videoCapture);
</code>
cameraProvider.bindToLifecycle(this, cameraSelector, preview, videoCapture);
for setting the camera to life cycle can you help me to fix it as well?
How to solve this?