CameraX Recorder.Builder 实现

huangapple go评论73阅读模式
英文:

CameraX Recorder.Builder Implementation

问题

我正在尝试创建一个用户可以录制视频的功能。但是我一直在遇到多个错误。我不理解文档。错误并不是特定的一个问题。只是我的实现和逻辑有缺陷。有关如何正确做到这一点的任何想法吗?任何帮助将不胜感激。我在这里卡了几天。

@SuppressLint("RestrictedApi")
private void startRecording(File videoFile) {
    this.videoFile = videoFile;

    Recorder.Builder recorderBuilder = new Recorder.Builder()
            .setExecutor(executor);

    // 使用 PreviewView 创建 VideoOutput
    VideoOutput videoOutput = new VideoOutput(viewFinder.getSurfaceProvider().getSurface()) {
    };

    VideoCapture videoCapture = new VideoCapture.Builder()
            .setOutputFile(videoFile)
            .setTargetRotation(viewFinder.getDisplay().getRotation())
            .build();

    videoCapture.startRecording(videoOutput, executor, videoRecordEvent -> {
        // 处理视频录制事件
        if (videoRecordEvent instanceof VideoRecordEvent.Start) {
            // 录制已开始
            runOnUiThread(() -> {
                // 如果需要,更新UI
            });
        } else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
            // 录制完成或遇到错误
            VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) videoRecordEvent;
            if (finalizeEvent.getError() != null) {
                // 发生错误
            }
        }
    });

    // 发送延迟消息,在60秒后停止视频
    videoStopHandler.postDelayed(videoStopRunnable, 60000);
}

private void stopRecording() {
    videoRecorder.stop();
    // 从处理程序中移除回调
    videoStopHandler.removeCallbacks(videoStopRunnable);
    isRecording = false;
}

private void startCamera() {
    executor = Executors.newSingleThreadExecutor();
    ProcessCameraProvider cameraProvider;
    try {
        cameraProvider = ProcessCameraProvider.getInstance(this);
        cameraProvider.addListener(() -> {
            try {
                bindPreview(cameraProvider.get());
            } catch (Exception e) {
                // 处理任何错误
            }
        }, ContextCompat.getMainExecutor(this));
    } catch (Exception e) {
        // 处理任何错误
    }
}
英文:

I'm trying to create a feature where a user can record videos. But I keep getting multiple errors. I don't understand the documentation. The error is not one specific thing. Just my implementation and logic are flawed. Any ideas on how to correctly do this? Any help would be much appreciated. I've been stuck here for days.

@SuppressLint("RestrictedApi")
private void startRecording(File videoFile) {
this.videoFile = videoFile;
Recorder.Builder recorderBuilder = new Recorder.Builder()
.setExecutor(executor);
// Create a VideoOutput using the PreviewView
VideoOutput videoOutput = new VideoOutput(viewFinder.getSurfaceProvider().getSurface()) {
};
VideoCapture videoCapture = new VideoCapture.Builder()
.setOutputFile(videoFile)
.setTargetRotation(viewFinder.getDisplay().getRotation())
.build();
videoCapture.startRecording(videoOutput, executor, videoRecordEvent -> {
// Handle video recording events
if (videoRecordEvent instanceof VideoRecordEvent.Start) {
// Recording started
runOnUiThread(() -> {
// Update UI if needed
});
} else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
// Recording finished or encountered an error
VideoRecordEvent.Finalize finalizeEvent = (VideoRecordEvent.Finalize) videoRecordEvent;
if (finalizeEvent.getError() != null) {
// Error occurred
}
}
});
// Post a delayed message to stop video after 60 seconds
videoStopHandler.postDelayed(videoStopRunnable, 60000);
}
private void stopRecording() {
videoRecorder.stop();
// Remove the callback from the handler
videoStopHandler.removeCallbacks(videoStopRunnable);
isRecording = false;
}
private void startCamera() {
executor = Executors.newSingleThreadExecutor();
ProcessCameraProvider cameraProvider;
try {
cameraProvider = ProcessCameraProvider.getInstance(this);
cameraProvider.addListener(() -> {
try {
bindPreview(cameraProvider.get());
} catch (Exception e) {
// Handle any errors
}
}, ContextCompat.getMainExecutor(this));
} catch (Exception e) {
// Handle any errors
}
}

答案1

得分: 1

我看不到 VideoCapture 的使用案例绑定。你可能忘记了吗?应该是这样的:(抱歉,这是 Kotlin 代码)

val recorder = Recorder.Builder()
    .setExecutor(cameraExecutor).setQualitySelector(qualitySelector)
    .build()
val videoCapture = VideoCapture.withOutput(recorder)

try {
    // 绑定用例到相机
    cameraProvider.bindToLifecycle(
            this, CameraSelector.DEFAULT_BACK_CAMERA, preview, videoCapture)
} catch(exc: Exception) {
    Log.e(TAG, "Use case binding failed", exc)
}

如果你不绑定它,它将无法工作。更多参考信息请查看这里:
https://developer.android.com/training/camerax/video-capture#using-videocapture-api

英文:

I can't see VideoCapture use case binding. Did you maybe forget it? It should be something like this: (Sorry it is in Kotlin)

val recorder = Recorder.Builder()
.setExecutor(cameraExecutor).setQualitySelector(qualitySelector)
.build()
val videoCapture = VideoCapture.withOutput(recorder)
try {
// Bind use cases to camera
cameraProvider.bindToLifecycle(
this, CameraSelector.DEFAULT_BACK_CAMERA, preview, videoCapture)
} catch(exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}

If you don't bind it, it would not work. For more reference check here:
https://developer.android.com/training/camerax/video-capture#using-videocapture-api

huangapple
  • 本文由 发表于 2023年6月2日 05:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定