如何使用React Native摄像机录制和保存视频?

时间:2018-06-21 06:57:27

标签: react-native-camera

我正在使用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>
      );
    }
  }

1 个答案:

答案 0 :(得分:0)

编辑。遵循recordAsync选项下的文档,您将发现pathhttps://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之后从诺言获得的数据。
对于图片,我使用RNFS.writeFile和base64数据