【问题标题】:Camera is not running相机未运行
【发布时间】:2020-02-02 06:07:10
【问题描述】:

我正在尝试一遍又一遍地制作原型应用程序

1- 用相机录制一段视频 x 秒

2- 显示此视频

为此,我使用来自 expo-camera 的组件 Camera 和来自 expo-av 的 Video

对此我有两种看法:

我在我的代码中使用 stateSequence 属性和 sequencer() 函数,它交替显示带有拍摄 x 秒的 Camera 组件的视图,以及允许我显示视频的视频视图。

Sequencer() 由 componentWillMount() 中的 setInterval( this.sequencer , 10000) 触发

我可以交替地从带有 Camera 组件的 View 切换到带有 Video 组件的 View。

要使用 Camera 组件录制视频,我使用 recordAsync(),但出现以下错误:

Unhandled promise rejection: Error: Camera is not running

我正在使用安卓手机进行测试。

你能帮帮我吗

这是我的代码

import { StyleSheet, Text, View ,TouchableOpacity} from 'react-native';

import * as Permissions from 'expo-permissions';
import { Camera } from 'expo-camera';
import { Video } from 'expo-av';
import { Logs } from 'expo';


export default class SequenceViewer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      stateSequence: "SHOOT ",
      hasCameraPermission: null,
      type: Camera.Constants.Type.front,
    }
    this.recordVideo = this.recordVideo.bind(this)
  }

  sequencer = () => {  

    if(this.state.stateSequence==="WATCH"){ 
      this.setState({  stateSequence: "SHOOT",})

      this.recordVideo();  //  Error message   Camera is not running

    } else {
      this.setState({stateSequence: "WATCH"})
    }


  }

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

          setInterval( this.sequencer , 10000);

        }  
      }
    }                  
  }

  recordVideo = async () => {

            if(this.state.cameraIsRecording){
              this.setState({cameraIsRecording:false})
              this.camera.stopRecording();
            }
            else {
              this.setState({cameraIsRecording:true})
              if (this.camera) {
                let record = await  this.camera.recordAsync(quality='480p',maxDuration=5,mute=true).then( data =>{

                  this.setState( {recVideoUri :data.uri})                         
                }) 
             }   

            }
  };  


  render() {

    const { hasCameraPermission } = this.state
      if(this.state.stateSequence=="WATCH")
      {
        return(
          <View style={styles.container}>
           <Video
              source={{ uri:this.state.recVideoUri }}
              rate={1.0}
              volume={1.0}
              isMuted={false}
              resizeMode="cover"
              shouldPlay
              isLooping
              style={{ width: 300, height: 300 }}
              ref={(ref) => { this.player = ref }}   
            />
        </View>  
        )

      } else
      {
        return(

          <View style={{ flex: 1 }}>
          <Camera style={{ flex: 1 }} type={this.state.type}  ref={ref => {this.camera = ref; }}></Camera>
        </View>

        )
      }
  }
}

const styles = StyleSheet.create({
    viewerText: {
        fontSize: 20,
        fontWeight: 'bold',
    },
    container: {
      flex: 1,
      backgroundColor: '#fff',
      alignItems: 'center',
      justifyContent: 'center',
    },
  });

谢谢

【问题讨论】:

  • 我也有同样的问题,你解决了吗?
  • 你有什么sdk???
  • 不,我不修复它,我使用 35 sdk

标签: android react-native expo


【解决方案1】:

我遇到了同样的问题,我的解决方案是默认相机类型必须是“后置”,您可以通过以下方式更改为“前置”:

componentDidMount = () => {
    this.props.navigation.addListener('didFocus', async () => {
      await setTimeout(() => {
        this.setState({ cameraType: Camera.Constants.Type.front })
      }, 100)
    });
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多