[未处理的承诺拒绝:TypeError:未定义不是一个对象(评估'_expoMediaLibrary.MediaLibrary.createAssetAsync')]
2020-08-22
1616
我使用
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个回答
我的相机应用也遇到了同样的问题。我通过更改导入解决了这个问题
// Change from this
import { MediaLibrary } from 'expo-media-library';
// To this
import * as MediaLibrary from 'expo-media-library';
Kennergf
2021-04-20