反应本机摄像机记录异步启动失败错误

时间:2019-06-03 16:08:40

标签: react-native react-native-camera

当我单击android设备上的“记录”按钮react-native-camera时,抛出错误“启动失败”。

Error: start failed.
    at createErrorFromErrorData (NativeModules.js:155)
    at NativeModules.js:104
    at MessageQueue.__invokeCallback (MessageQueue.js:414)
    at MessageQueue.js:127
    at MessageQueue.__guard (MessageQueue.js:314)
    at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:126)
    at debuggerWorker.js:80
  1. 权限代码已添加到AndroidManifest.xml文件中
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. 使用PermissionsAndroid授予所有权限。
  2. 尝试过Link中提到的合格质量选项
setCameraReady = () => {
    this.setState({
      cameraReady: true
    });
};
startRecording = async () => {
    const { cameraReady } = this.state;
    if (cameraReady) {
      try {
        // default to mp4 for android as codec is not set
        const { uri, codec = 'mp4' } = await this.camera.recordAsync({
          maxDuration: 30
        });
        console.log(uri)
      } catch (error) {
        console.log(error);
      }
    }
  };
 stopRecording = () => {
    this.camera.stopRecording();
  };
<RNCamera
    style={cameraStyle}
    ref={(ref) => {
      this.camera = ref;
    }}
    type={cameraType}
    mirrorImage={cameraType !== 'back'}
    androidCameraPermissionOptions={{
      title: 'Permission to use camera',
      message: 'We need your permission to use your camera phone',
      buttonPositive: 'Ok',
      buttonNegative: 'Cancel'
    }}
    androidRecordAudioPermissionOptions={{
      title: 'Permission to use audio recording',
      message: 'We need your permission to use your audio',
      buttonPositive: 'Ok',
      buttonNegative: 'Cancel'
    }}
    onCameraReady={this.setCameraReady}
   />

在recordAsync promise被解析后应该获取URI。

1 个答案:

答案 0 :(得分:0)

感谢反应本地相机团队。

将react-native-camera更新到最新版本(即1.10.0)解决了该问题。