如何访问反应原生摄像机的视频功能?



我一直在试图使反应式相机的视频功能工作,但是尝试了大量方法,但是继续遇到相同的错误。这是我的代码:

class MainCamera extends Component {
  constructor() {
  super();
  this.render = this.render.bind(this)
  this.state = { cameraType: Camera.constants.Type.back }
}
  render() {
return (
  <View style={styles.container}>
    <Camera
      ref='camera'
      style={styles.preview}
      aspect={Camera.constants.Aspect.fill}
      type={this.state.cameraType}
      captureMode={Camera.constants.CaptureMode.video}
      captureAudio={false}
      target={Camera.constants.CaptureTarget.disk}>
      <TouchableHighlight
        onPressIn={this.onPressIn.bind(this)}
        onPressOut={this.stopVideo.bind(this)}>
        <Icon name="video-camera" size={40} />
      </TouchableHighlight>
    </Camera>
  </View>
);
  }
onPressIn() {
  recordVideo = setTimeout(this.takeVideo.bind(this), 100);
}
takeVideo() {
    this.refs.camera.capture({
      target: Camera.constants.CaptureTarget.disk
    })
      .then(data => {
        console.log(data);
      })
      .catch(err => console.log(err));
  }
stopVideo() {
  this.refs.camera.stopCapture({})
    .then(data => console.log(data))
    .catch(err => console.log(err));
  }
}

当我在stopcapture()方法上使用'。then'诺言时,我会收到错误"无法读取属性',然后"未定义",但是如果我不添加'。而且我没有收到任何数据。有人有任何建议吗?

takeVideo() {
    this.refs.camera.capture({
      audio: true,
      mode: Camera.constants.CaptureMode.video,
      target: Camera.constants.CaptureTarget.disk
    })
      .then((data) => {
        console.log(data);
      })
      .catch((err) => console.log(err));
  }
stopVideo() {
  this.refs.camera.stopCapture();
}

stopCapture()功能不是承诺。

不幸失去旧文件后的新组件:

 class VideoCamera extends Component {
  constructor() {
    super()
    this.state = {
      captureMode: Camera.constants.CaptureMode.video,
      captureAudio: false,
      captureTarget: Camera.constants.CaptureTarget.cameraRoll,
    }
  }
  render() {
    return (
      <View style={styles.container}>
        <Camera
            aspect={Camera.constants.Aspect.fill}
            captureAudio={this.state.captureAudio}
            captureMode={this.state.captureMode}
            captureTarget={this.state.captureTarget}
            ref="camera"
            style={styles.preview}
        >
        <TouchableHighlight
            onPressIn={this._startRecord.bind(this)}
            onPressOut={this._endVideo.bind(this)}
        >
        <Icon
           name={'video-camera'}
           size={40}
           style={styles.recordButton}
        />
          </TouchableHighlight>
          </Camera>
         </View>
          )
      }
  _startRecord() {
    startVideo = setTimeout(this._recordVideo.bind(this), 50)
  }
  _recordVideo() {
    this.refs.camera.capture({})
      .then((data) => console.log(data))
      .catch((err) => console.log(err))
   }
  _endVideo() {
   this.refs.camera.stopCapture()
  }
}

摄像机打开,创建2个按钮以启动并停止下面的视频。

                <View style={styles.container}>
                    <RNCamera
                        ref={ref => {
                            this.camera = ref;
                        }}
                        style={styles.preview}
                        type={RNCamera.Constants.Type.back}
                        flashMode={RNCamera.Constants.FlashMode.on}
                        androidCameraPermissionOptions={{
                            title: 'Permission to use camera',
                            message: 'We need your permission to use your camera',
                            buttonPositive: 'Ok',
                            buttonNegative: 'Cancel',
                        }}
                        androidRecordAudioPermissionOptions={{
                            title: 'Permission to use audio recording',
                            message: 'We need your permission to use your audio',
                            buttonPositive: 'Ok',
                            buttonNegative: 'Cancel',
                        }}
                        onGoogleVisionBarcodesDetected={({ barcodes }) => {
                            console.log(barcodes);
                        }}
                        captureAudio={true}
                    />  
       <View style={{ flex: 0, flexDirection: 'row', justifyContent: 'center' }}>
                <TouchableOpacity onPress={this.takeVideo.bind(this)} style={styles.capture}>
                    <Text style={{ fontSize: 14 }}> VIDEO </Text>
                </TouchableOpacity>
                <TouchableOpacity onPress={this.stoprec.bind(this)} style={styles.capture}>
                    <Text style={{ fontSize: 14 }}> STOP </Text>
                </TouchableOpacity>
            </View>

还创建两种方法来录制视频并停止录制如下。下面的方法在上面的按钮中调用。

 takeVideo = async () => {
        if (this.camera) {
            try {
                const options = {
                    quality: 0.5,
                    videoBitrate: 8000000,
                    maxDuration: 30
                };
                const promise = this.camera.recordAsync(options);
                if (promise) {
                    this.setState({ recording: true });
                    const data = await promise;
                    this.setState({ recording: false });
                }
            } catch (error) {
                console.log(error);
            }
        }
    }
//stop the recording by below method
    stoprec = async () => {
        await this.camera.stopRecording();
    }

,最后,如果您想要文件路径,并且您将获得的所有内容作为data.uri

谢谢。希望它能提供清晰的图片

语法错误:

then((data) => {
    console.log(data);
  })
((data)=>{}) should be done instead of (data=>{}).

最新更新