我正在使用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>
);
}
}发布于 2019-06-05 17:23:39
编辑。按照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获得的数据。
对于图片,我使用带有base64数据的RNFS.writeFile
https://stackoverflow.com/questions/50962237
复制相似问题