[未处理的承诺拒绝:TypeError:未定义不是对象(正在评估'_expoMediaLibrary.MediaLibrary.createAssetAsync')

时间:2020-08-22 03:25:31

标签: reactjs react-native expo react-native-android expo-camera

我使用expo-camera录制视频并将其保存在设备中,但是即使停止录制它也具有视频uri,.MediaLibrary.createAssetAsync仍出现上述错误,并且从第一个开始就停止了录制即使我已经使用setTimeout停止了录制,这也是我的相机代码:

import React, { useState, useEffect, useRef } from 'react';
import { Text, View } from 'react-native';
import { Camera } from 'expo-camera';
import { MediaLibrary } from 'expo-media-library';
import { Audio } from 'expo-av';

export default function App() {
  const [hasPermission, setHasPermission] = useState(null);
  const [camera, setCamera] = useState(null);
  const [recording, setRecording] = useState(false);
  const [video, setVideo] = useState(null);
  const [stop, setStop] = useState(false);

  const recordingVideo = async () => {
    const video = await camera.recordAsync();
    console.log(video);
    setVideo(video);
  }

  const saveVideo = async () => {
    console.log('uri', video.uri);
    const asset = await MediaLibrary.createAssetAsync(video.uri);
    if (asset) {
      console.log('asset', asset);
      setVideo(null);
    }
  };

  useEffect(() => {
    console.log('recoring', recording);
    if (recording && camera) recordingVideo();
  }, [recording, camera]);

  useEffect(() => {
    console.log('stop', stop);

    if (stop) {
      setRecording(false);
      camera.stopRecording();
      saveVideo();
    }
  }, [stop]);

  useEffect(() => {
    (async () => {
      const { status } = await Camera.requestPermissionsAsync();
      const audioPerm = await Audio.requestPermissionsAsync();
      setHasPermission(status === 'granted' && audioPerm.status === 'granted');
    })();
  }, []);

  useEffect(() => {
    if(camera) {
      console.log('ref');
      setRecording(true);

      setTimeout(() => {
        setStop(true);
      }, 10000);
    }
  }, [camera]);

  if (hasPermission === null) {
    return <View />;
  }

  if (hasPermission === false) {
    return <Text>No access to camera or audio</Text>;
  }

  return (
    <View style={{ flex: 1 }}>
      <Camera style={{ flex: 1 }} type={Camera.Constants.Type.front} ref={ref => setCamera(ref)}>
        <View style={{ flex: 1, backgroundColor: '#00000000', justifyContent: 'center' }}>
          <Text style={{ marginHorizontal: 40 }}>{recording ? 'Recording' : !stop ? 'Waiting for recording' : 'recording finished' }</Text>
        </View>
      </Camera>
    </View>
  );
}

我在expo-media-library文档中进行了搜索,并记录了video.uri,它恰好是一个匹配的参数,但无法理解为什么它如此工作。

1 个答案:

答案 0 :(得分:0)

我在我的相机应用中遇到了同样的问题。我通过更改导入解决了

{{1}}