How do I access the react-native-camera's vide

2019-06-08 15:20发布

问题:

I've been attempting to get the react-native-camera's video feature to work, but and have tried a vast number of methods but keep getting the same errors. Here is my code:

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

When I use the '.then' promise on the stopCapture() method, I receive the error "Cannot read property 'then' of undefined", but if I don't add the '.then', then nothing happens and I don't receive any data. Does anybody have any suggestions?

回答1:

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

The stopCapture() function is not a promise.



回答2:

syntax error:

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

((data)=>{}) should be done instead of (data=>{}).


回答3:

The new component after the unfortunate loss of the old file:

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

}