react-native - React Native Video 未加载本地文件
问题描述
我正在向我的 react 本机应用程序添加视频录制和预览功能。为了拍摄视频,我使用了以下代码,一旦录制,我就可以获取输出文件。现在使用文件 uri,我尝试使用 react 向用户预览这个文件-native-video 组件。但它总是显示空白屏幕,但没有加载视频。
import { RNCamera, FaceDetector } from 'react-native-camera';
<RNCamera
ref={ref => {
this.camera = ref;
}}
style={{width:width(100),height:height(100)}}
type={RNCamera.Constants.Type.back}
flashMode={RNCamera.Constants.FlashMode.on}
permissionDialogTitle={"Permission to use camera"}
permissionDialogMessage={
"We need your permission to use your camera phone"
}
>
async startRecording() {
this.setState({ recording: true });
var self = this;
this.camera.recordAsync().
then((data) => {
self.setState({video:data.uri})
})
}
一旦视频变量可用,我尝试使用下面的代码片段加载它:-
import Video from 'react-native-video';
{this.state.video !== ''
?
<Video source={{uri: this.state.video}} // Can be a URL or a local file.
ref={(ref) => {
this.player = ref
}}
onBuffer={this.onBuffer}
onError={this.videoError}
style={styles.backgroundVideo} />
:
<View/>
}
版本详细信息是:-
- “反应原生”:“0.61.5”。
- “反应原生视频”:“^5.0.2”
- “反应本机相机”:“^3.15.0”
这是相机录制完成后我得到的视频文件“file:///data/user/0/com.dezzexvideo/cache/Camera/94d367ef-c6b8-43c3-844f-7d0d2c6d0ddf.mp4”
解决方案
您提到的文件路径file:///data/user/0/com.dezzexvideo/cache/Camera/94d367ef-c6b8-43c3-844f-7d0d2c6d0ddf.mp4 - 似乎是缓存目录文件路径。
所以可能你不能直接从缓存目录播放视频。你可以使用react-native-fs。
使用RNFS
首先检查文件是否存在于缓存路径中
if(await RNFS.exists(cachefilepath)){
//then copy the video file to Document Directory using RNFS
//copyFile(filepath: string, destPath: string): Promise<void>
RNFS.copyFile(cachefilepath, RNFS.DocumentDirectoryPath).then({
})
}
//Play file from directory path
<Video source={{uri: RNFS.DocumentDirectoryPath+filename}} ... />
推荐阅读
- python - Pyramid 项目的 .jinja 模板中的 Vue.js
- java - Java:使用第一个数组的前 3 个整数,然后使用第二个数组的 3 个整数将 2 个数组组合成第三个数组
- r - 使用库(gpg)加密/解密 R 中的对象
- wordpress - 如何根据 WordPress 中另一个下拉列表的值更改下拉列表的内容?
- powershell - 检查文件是否存在于监视文件夹中
- c++ - 将递归算法生成所有组合转换为迭代
- javascript - 描述之外的 beforeEach 是否总是在描述内部的 beforeEach 开始之前完成?
- pytorch - 使用 Dev Pytorch 1.0 将 Pytorch 模型加载到 C++ 中
- html - 使用 html 5“禁用”时返回的参数中的空数组
- javascript - 更改 React-Table 默认样式