1

我一直在尝试让 react-native-camera 的视频功能正常工作,但是我尝试了很多方法,但总是遇到同样的错误。这是我的代码:

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' 承诺时,我收到错误“无法读取未定义的属性 'then'”,但如果我不添加 '.then',则什么也不会发生,我不会'没有收到任何数据。有人有什么建议吗?

4

4 回答 4

0

语法错误:

then((data) => {
    console.log(data);
  })

((data)=>{}) should be done instead of (data=>{}).
于 2016-06-22T11:11:48.980 回答
0
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()功能不是承诺。

于 2016-06-26T21:35:33.323 回答
0

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

 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()
  }

}
于 2016-07-18T02:47:17.140 回答
0

打开相机,创建 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

谢谢你。希望能给个清晰的图

于 2019-09-25T13:38:49.707 回答