gpt4 book ai didi

react-native-camera - 如何使用 React Native 相机录制和保存视频?

转载 作者:行者123 更新时间:2023-12-01 05:54:02 26 4
gpt4 key购买 nike

我正在使用 react-native-camera RNCamera 组件来录制视频。主要任务是在聊天窗口录制上传和预览,但是我无法获取视频。
RNCamera recordAsync(options)命令将其作为缓存保存到本地缓存文件夹中,我无法从文件夹中获取它。我尝试使用 fetch-blob 但它返回的文件大小为 0。我做错了什么?

import {RNCamera} from 'react-native-camera';
import RNFetchBlob from 'react-native-fetch-blob';
constructor(props) {
super(props);
this.state = {
path: "",
recorded: false,
isRecording: false,
}
}

takeVideo() {
this.state.isRecording ? this.stopRecord() : this.saveVideo();
this.state.isRecording ? this.setState({isRecording: false}) : this.setState({isRecording: true})
}

async stopRecord(){
this.camera.stopRecording();
const { config, fs, android } = RNFetchBlob;
const str = this.state.path;
const fileName = str.substr(str.indexOf("Camera")+7, str.indexOf(".mp4"));
const path = fs.dirs.CacheDir + '/Camera/'+fileName;

const res = await fetch(str);
const blob = await res.blob();

console.log("BLOB", blob); // -- it returns -
/*
'BLOB', { _data:
{ name: '07395974-32e0-4e43-8f7c-af5ec2d7267b.mp4',
type: 'video/mp4',
size: 0,
lastModified: 0,
offset: 0,
lobId: '445db879-9795-4cd9-a8cf-d33d60a41a13' } }
*/
}

async saveVideo () {
if (this.camera) {
const options = { maxDuration: 10 }
const data = await this.camera.recordAsync(options)
this.setState({path: data.uri })
this.setState({recorded: false})
console.log("FILE", data.uri); // -- it returns -

//'FILE', 'file:///data/user/0/com.project/cache/Camera/07395974-32e0-4e43-8f7c-af5ec2d7267b.mp4'
}
};

render() {
return (
<View style = {[styles.root, this.props.contentStyle]}>
{this.props.header}
<RNCamera
// type = {RNCamera.Constants.Type.front}
ref = {camera => {this.camera = camera}}
style = {styles.preview}
captureAudio = {true}
ratio = "16:9"
permissionDialogTitle={'Permission to use camera'}
permissionDialogMessage={'We need your permission to use your camera phone'}
>
<View style = {{flexDirection: 'column', alignItems:'center'}}>
<TouchableOpacity onPress = {this.takeVideo}>
<Text style = {{backgroundColor: 'white'}}>[CAPTURE]</Text>
</TouchableOpacity>
</View>
</RNCamera>
</View>
);
}
}

最佳答案

编辑。按照 recordAsync 选项下的文档,你会发现 path :https://github.com/react-native-community/react-native-camera/blob/master/docs/RNCamera.md

这就是我使用 RNFS from 'react-native-fs' 保存它的方式:

let fileName = 'VID_currentDate.mp4';
RNFS.copyFile(data.uri, RNFS.PicturesDirectoryPath + '/Videos/' + fileName).then(() => {
console.log("Video copied locally!!");
}, (error) => {
console.log("CopyFile fail for video: " + error);
});`
data.uri是你在 recordAsync之后从promise得到的数据.
对于图片,我正在使用 RNFS.writeFile使用 base64 数据

关于react-native-camera - 如何使用 React Native 相机录制和保存视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50962237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com