简体   繁体   English

Expo Camera recordAsync承诺无法解决

[英]Expo Camera recordAsync promise not resolving

Im starting to develop a mobile application with expo/react native, but I'm having some problems handling the camera object:我开始使用 expo/react native 开发移动应用程序,但是我在处理相机对象时遇到了一些问题:

I have a camera object that I start recording (recordAsync) at componentDidMount and I stop it (stopRecording) at componentWillUnmount.我有一个相机对象,我在 componentDidMount 开始录制 (recordAsync),然后在 componentWillUnmount 停止它 (stopRecording)。 however the promise is never resolved (neither the then, catch no finally are called)然而承诺永远不会解决(无论 then 还是 catch no finally 都被调用)

am I doing something wrong?难道我做错了什么? here's the code:这是代码:

import { Camera, Permissions } from 'expo';

import React from 'react';


export default class CameraReaction extends React.Component {
  constructor(props){
    super(props)
    this.takeFilm = this.takeFilm.bind(this)       
    this.isFilming=false
    this.cameraScreenContent = this.renderCamera()
  }

  componentDidMount(){
    if (this.props.shouldrecording && !this.isFilming ){
      this.takeFilm()
    }
  }
  componentWillUnmount(){
    this.camera.stopRecording()
  }

  saveMediaFile = async video => {
    console.log("=======saveMediaFile======="); 
  }

  renderCamera = () => {
    let self = this
    return (
      <View style={{ flex: 1 }}>
        <Camera
          ref={ref => {self.camera=ref}}
          style={styles.camera}
          type='front'
          whiteBalance='off'
          ratio='4:3'
          autoFocus='off'
          >
        </Camera>
      </View>
    );
  }

  takeFilm(){
    let self = this
    try{
      self.camera.recordAsync()
      .then(data => {
        self.saveMediaFile(data),
        self.isFilming=false
      })
      .catch(error => {console.log(error)})
      this.isFilming = true
    }
    catch(e){      
      this.isFilming = false      
    }            
  };

  render() {    
    return <View style={styles.container}>{this.cameraScreenContent}</View>;
  }

}

anyone has any clue of what I'm doing wrong?任何人都知道我做错了什么?

thanks in advance提前致谢

I finally realised that we can't start recording directly when a component is rendered.我终于意识到我们不能在组件渲染时直接开始录制。 An by 'directly' I mean without any further action from the user. “直接”是指无需用户采取任何进一步行动。 If I do it in two steps (pe waiting for the user to click somewhere), if works perfectly.如果我分两步完成(pe 等待用户点击某处),如果工作完美。 But I don't see any reference to this behaviour / limitation in the documentation.但是我在文档中没有看到任何对这种行为/限制的引用。

The working code bellow:工作代码如下:

import React from 'react';
import { StyleSheet, Text, View , TouchableOpacity} from 'react-native';
import { Camera, Permissions} from 'expo';

export default class App extends React.Component {
  constructor(props){
    super(props)    
    this.camera=undefined
    this.state = {permissionsGranted:false,bcolor:'red'}
    this.takeFilm = this.takeFilm.bind(this)
  }

  async componentWillMount() {    
    let cameraResponse = await Permissions.askAsync(Permissions.CAMERA)
    if (cameraResponse.status == 'granted'){
      let audioResponse = await Permissions.askAsync(Permissions.AUDIO_RECORDING);
      if (audioResponse.status == 'granted'){
        this.setState({ permissionsGranted: true });
      }
    }                  
  }

  takeFilm(){    
    let self = this;
    if (this.camera){
      this.camera.recordAsync().then(data => self.setState({bcolor:'green'}))
    }    
  }

  render() {    
    if (!this.state.permissionsGranted){
      return <View><Text>Camera permissions not granted</Text></View>
    } else {
      return (
        <View style={{flex: 1}}>
          <View style={{ flex: 1 }}>
            <Camera ref={ref => this.camera = ref} style={{flex: 0.3}} ></Camera>
          </View>
          <TouchableOpacity style={{backgroundColor:this.state.bcolor, flex:0.3}} onPress={() => {

            if(this.state.cameraIsRecording){
              this.setState({cameraIsRecording:false})
              this.camera.stopRecording();
            }
            else{
              this.setState({cameraIsRecording:true})
              this.takeFilm();
            }
          }} />
        </View>)
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Expo 相机的 takePictureAsync 不返回任何内容。 (承诺从未兑现) - takePictureAsync of Expo's Camera returns nothing. (Promise never fulfilled) Expo 中的相机预览失真 - Camera Preview in Expo is Distorted 使用接口回调的 ReactNative NativeModules 未解析 promise - ReactNative NativeModules using Interface callback not resolving promise 世博相机 takePictureAsync() 在 Android 上不起作用 - expo-camera takePictureAsync() not working on Android "未定义不是对象(评估 &#39;_$$_REQUIRE(_dependencyMap[5], &quot;expo-camera&quot;).Camera&#39;)" - Undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[5], "expo-camera").Camera') 延迟捕获图像 - React Native Camera / Expo Camera - Delay on the capture of an image - React Native Camera / Expo Camera 错误:expo-camera.isAvailableAsync 和 expo-camera.getAvailableCameraTypesAsync 在 android 上不可用 - errors: expo-camera.isAvailableAsync and expo-camera.getAvailableCameraTypesAsync is not available on android 未处理的 promise 拒绝:TypeError:网络请求失败 expo 节点后端 - Unhandled promise rejection: TypeError: Network request failed expo node backend 如何在反应原生 expo 应用程序(android)中授予相机权限 - How to grant camera permission in react native expo app (android) EXPO 从相机拍摄后将照片保存在图片中 - EXPO save photo in Pictures after taking it from Camera
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM