英文:
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<VideoRecordEvent>() {
@Override
public void accept(VideoRecordEvent videoRecordEvent) {
if (videoRecordEvent instanceof VideoRecordEvent.Start) {
setBackgroundBtn(R.drawable.recoding);
Toast.makeText(CameraActivity.this, "recoding start...", Toast.LENGTH_SHORT).show();
} else if (videoRecordEvent instanceof VideoRecordEvent.Finalize) {
setBackgroundBtn(R.drawable.round_bg);
Toast.makeText(CameraActivity.this, "Recoding stop ...", 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, "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);
}
}
}
}
答案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, "Recoding stop ...", 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<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, "Recoding stop ...", Toast.LENGTH_SHORT).show();
}
}
});
Or recordEvent.outputResults.outputUri
CameraX Codelab on event finalize
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论