首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用react本地摄像头录制和保存视频?

如何使用react本地摄像头录制和保存视频?
EN

Stack Overflow用户
提问于 2018-06-21 14:57:27
回答 1查看 4.9K关注 0票数 6

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

代码语言:javascript
复制
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>
      );
    }
  }
EN

回答 1

Stack Overflow用户

发布于 2019-06-05 17:23:39

编辑。按照recordAsync选项下的文档操作,您将找到pathhttps://github.com/react-native-community/react-native-camera/blob/master/docs/RNCamera.md

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

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50962237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档