相机还没有准备好!等待onInitialized()



我正试图将react-native-vision-camera实现到我的React Native应用程序中,它在iOS上完美地工作,但在Android上它拒绝正常工作。在Android上,我看到如下错误:

[session/camera-not-ready] The Camera is not ready yet! Wait for the onInitialized() callback!

从我所看到的,这个错误发生在权限验证之前被调用时,这不是我的应用程序的情况。考虑以下…

// Camera Permission State
const [cameraPerm, setCameraPerm] = useState(false)
// Checks Mic and Video Permissions as soon as page loads
useEffect(() => {
checkPermissions();
}, []);

// Called in a useEffect to gain permissions
const checkPermissions = async () => {
// Request Permissions on component load
await Camera.requestCameraPermission();
await Camera.requestMicrophonePermission();
await requestPermission()
const cameraPermission = await Camera.getCameraPermissionStatus();
setCameraPerm(cameraPermission)
const microphonePermission = await Camera.getMicrophonePermissionStatus();
};

// Renders The Camera, if permissed and not done recording
function renderCameraScreen(){
// No permissions
if (device == null) return <View style={{backgroundColor: 'black'}}/>;
// Video Recorded
if (recorded){
return(
<View>
<Text style={{...FONTS.Title, textAlign: 'center', marginTop: 100}}>
Video Recorded
</Text>
</View>
)
}
if (!cameraPerm){
return(
<View>
<Text style={{...FONTS.Title, textAlign: 'center', marginTop: 100}}>
Please enable video permissions from your settings to access the camera
</Text>
</View>
)
}

return (
<View style={{backgroundColor: 'black', height: maxHeight * 0.70,}}>
<Camera
style={StyleSheet.absoluteFill}
device={device}
isActive={true}
video={true}
audio={true}
ref={camera}
/>
<View style={{alignSelf: 'center', marginTop: maxHeight * 0.65}}>
{renderRecordButton()}
</View>
</View>
);
};

考虑到这一点,我不知道为什么我得到这个错误。说实话,我也无法在任何文档中找到任何提到onInitialized()的内容,所以我不确定我将如何利用该功能,以及在哪里。

根据https://github.com/mrousavy/react-native-vision-camera/issues/789也许你需要隐藏摄像头,直到你的应用程序被授权使用摄像头。

我在Android上使用react-native-vision camera录制音频和视频时也遇到了同样的问题。

我的解决方案是使用PermissionsAndroid:

下面是我的代码片段:
import {PermissionsAndroid} from 'react-native';
const getAllPermissions = useCallback(async () => {
PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.CAMERA,
PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
]).then(result => {
if (
result['android.permission.CAMERA'] === 'granted' &&
result['android.permission.RECORD_AUDIO'] === 'granted'
) {
setLoading(devices);
} else {
Alert.alert(
'Permission Not Granted',
'You need to grant this app permission to use the camera to record audio and video',
[
{
text: 'Cancel',
onPress: () => navigation.pop(),
},
{
text: 'Grant',
onPress: () => Linking.openSettings(),
},
],
);
setLoading(null);
}
});
}, [navigation, devices]);
useEffect(() => {
getAllPermissions();
}, [getAllPermissions, devices]);

最新更新