got an error in camerax in android with java – The video frame producer became inactive before any data was received

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật