如何在使用 CameraX 捕获视频后获取 savedUri?

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

How to get savedUri after capture video in CameraX?

问题

我想从CameraX的CaptureVideo中获取URI。

这是我的视频捕获代码:

private void takeVideo() {
    if (mso != null) {
        if (recording != null) {
            recording.close();
            recording = null;
            return;
        }
        recording = videoCapture.getOutput().prepareRecording(CameraActivity.this, mso)
                .start(ContextCompat.getMainExecutor(this), new Consumer<VideoRecordEvent>() {
                    @Override
                    public void accept(VideoRecordEvent videoRecordEvent) {
                        if (videoRecordEvent instanceof VideoRecordEvent.Start) {
                            setBackgroundBtn(R.drawable.recoding);
                            Toast.makeText(CameraActivity.this, "录制开始...", Toast.LENGTH_SHORT).show();
                        } else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
                            setBackgroundBtn(R.drawable.round_bg);
                            Toast.makeText(CameraActivity.this, "录制停止...", Toast.LENGTH_SHORT).show();
                            // 在这里显示URI
                            String uriString = recording.mRecorder.mOutputUri.uriString;
                        }
                    }
                });
    }
}

URI应该在事件VideoRecordEvent.Finalize触发时返回。我在调试时在触发VideoRecordEvent.Finalize时显示URI,它位于recording.mRecorder.mOutputUri.uriString中。

但是我不知道如何获取这个值,因为没有提供获取URI的函数。

英文:

I want to get uri from CaptureVideo of CameraX

Here is my code for capture video

private void takeVideo() {
    if (mso != null) {
        if (recording != null) {
            recording.close();
            recording = null;
            return;
        }
        recording = videoCapture.getOutput().prepareRecording(CameraActivity.this, mso)
                .start(ContextCompat.getMainExecutor(this), new Consumer&lt;VideoRecordEvent&gt;() {
                    @Override
                    public void accept(VideoRecordEvent videoRecordEvent) {
                        if (videoRecordEvent instanceof VideoRecordEvent.Start) {
                            setBackgroundBtn(R.drawable.recoding);
                            Toast.makeText(CameraActivity.this, &quot;recoding start...&quot;, Toast.LENGTH_SHORT).show();
                        } else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
                            setBackgroundBtn(R.drawable.round_bg);
                            Toast.makeText(CameraActivity.this, &quot;Recoding stop ...&quot;, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}

The uri should return somewhere on event VideoRecordEvent.Finalize
I debugged recording and show uri when trigger VideoRecordEvent.Finalize and the uri is in recoding.mRecorder.mOutputUri.uriString

But I have no idea how to get this value because no function to get that uri.

答案1

得分: 0

你可以检查录制是否不为null,如果是,那么意味着视频录制已成功启动。然后从录制对象中获取outputFileOptions,这个对象包含你要查找的URI。然后,要获取Uri,你需要将outputFileOptions转换为OutputFileUriOptions,并使用getUri()方法来检索URI,如果你想将它作为String,则需要使用toString()添加在后面。

@Override
public void accept(VideoRecordEvent videoRecordEvent) {
    if (videoRecordEvent instanceof VideoRecordEvent.Start) {
        setBackgroundBtn(R.drawable.recoding);
        Toast.makeText(CameraActivity.this, "recording start...", Toast.LENGTH_SHORT).show();
    } else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
        setBackgroundBtn(R.drawable.round_bg);
        Toast.makeText(CameraActivity.this, "Recording stop ...", Toast.LENGTH_SHORT).show();

        if (recording != null) {
            OutputFileOptions outputFileOptions = recording.getOutputFileOptions();
            if (outputFileOptions instanceof OutputFileOptions.OutputFileUriOptions) {
                Uri outputFileUri = ((OutputFileOptions.OutputFileUriOptions) outputFileOptions).getUri();
                String outputFileUriString = outputFileUri.toString();
                Log.d("CameraX", "Video output URI: " + outputFileUriString);
            }
        }
    }
}
英文:

What you can do is check if recording is not null, then it means that the video recording was successfully started. Then the outputFileOptions from the recording object that is the one that contains the URI you are looking for. Then to get the Uri you need to cast the outputFileOptions to OutputFileUriOptions and retrieve the URI using the getUri() method, then if you want it as String you have to add the toString() to it.

@Override
public void accept(VideoRecordEvent videoRecordEvent) {
    if (videoRecordEvent instanceof VideoRecordEvent.Start) {
        setBackgroundBtn(R.drawable.recoding);
        Toast.makeText(CameraActivity.this, &quot;recording start...&quot;, Toast.LENGTH_SHORT).show();
    } else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
        setBackgroundBtn(R.drawable.round_bg);
        Toast.makeText(CameraActivity.this, &quot;Recording stop ...&quot;, Toast.LENGTH_SHORT).show();

        if (recording != null) {
            OutputFileOptions outputFileOptions = recording.getOutputFileOptions();
            if (outputFileOptions instanceof OutputFileOptions.OutputFileUriOptions) {
                Uri outputFileUri = ((OutputFileOptions.OutputFileUriOptions) outputFileOptions).getUri();
                String outputFileUriString = outputFileUri.toString();
                Log.d(&quot;CameraX&quot;, &quot;Video output URI: &quot; + outputFileUriString);
            }
        }
    }
}

答案2

得分: 0

I found answer, I use getOutputResults().getOutputUri()

recording = videoCapture.getOutput().prepareRecording(CameraActivity.this, mso)
.start(ContextCompat.getMainExecutor(this), new Consumer<VideoRecordEvent>() {
@Override
public void accept(VideoRecordEvent videoRecordEvent) {
if (videoRecordEvent instanceof VideoRecordEvent.Start) {
// do something
} else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
...
Uri result = ((VideoRecordEvent.Finalize) videoRecordEvent).getOutputResults().getOutputUri();
Toast.makeText(CameraActivity.this, "result url: " + result.toString(), Toast.LENGTH_LONG).show();

            Toast.makeText(CameraActivity.this, &quot;Recoding stop ...&quot;, Toast.LENGTH_SHORT).show();
        }
    }
});

Or recordEvent.outputResults.outputUri CameraX Codelab on event finalize

英文:

I found answer, I use getOutputResults().getOutputUri()

recording = videoCapture.getOutput().prepareRecording(CameraActivity.this, mso)
                .start(ContextCompat.getMainExecutor(this), new Consumer&lt;VideoRecordEvent&gt;() {
                    @Override
                    public void accept(VideoRecordEvent videoRecordEvent) {
                        if (videoRecordEvent instanceof VideoRecordEvent.Start) {
                            // do something
                        } else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
                            ...
                            Uri result = ((VideoRecordEvent.Finalize) videoRecordEvent).getOutputResults().getOutputUri();
                            Toast.makeText(CameraActivity.this, &quot;result url: &quot; + result.toString(), Toast.LENGTH_LONG).show();

                            Toast.makeText(CameraActivity.this, &quot;Recoding stop ...&quot;, Toast.LENGTH_SHORT).show();
                        }
                    }
                });

Or recordEvent.outputResults.outputUri CameraX Codelab on event finalize

huangapple
  • 本文由 发表于 2023年2月27日 18:11:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579125.html
匿名

发表评论

匿名网友

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

确定