我在我的 React Native 应用程序中使用 expo-camera 进行视频录制。在开发中一切都运行良好,但是当我生成APK文件时,相机无法录制视频。 我使用了 useRef 钩子,它是我的代码,当单击开始录制按钮时触发并触发 startRecord() 函数,该函数使用 常量记录视频=等待cameraRef.current.recordAsync();。 我认为也许 cameraRef.current.recordAsync() 在生产中不起作用。
任何建议或解决方案都会非常有帮助。
谢谢!
**Video Recording Component**
import React, { useState, useEffect, useRef } from "react";
import { View, Text, TouchableOpacity, StyleSheet, Alert } from "react-native";
// packages
import { Camera } from "expo-camera";
import * as FileSystem from "expo-file-system";
import {
widthPercentageToDP as wp,
heightPercentageToDP as hp,
} from "react-native-responsive-screen";
// Functional Component
const Recordvideo = (props) => {
// Ref
const cameraRef = useRef(null);
// States
const [video, setVideo] = useState(null);
const [recording, setRecording] = useState(false);
const [hasPermission, setHasPermission] = useState(null);
// getting camera permission
useEffect(() => {
getPermission();
}, [getPermission]);
const getPermission = async () => {
const { status } = await Camera.requestPermissionsAsync();
if (status === "granted") {
setHasPermission(true);
}
};
// start/stop video recording
const toogleRecord = () => {
if (recording) {
stopRecord();
} else {
startRecord();
}
};
// start recording
const startRecord = async () => {
if (cameraRef.current) {
setRecording(true);
const recordedVideo = await cameraRef.current.recordAsync();
setVideo(recordedVideo);
}
};
// stop recording
const stopRecord = async () => {
setRecording(false);
cameraRef.current.stopRecording();
};
// saving recorded video
const saveVideo = async () => {
let fileInfo = await FileSystem.getInfoAsync(video.uri);
if (fileInfo.size <= 5242880) {
props.navigation.push("Post", {
VIDEOURL: video.uri,
VIDEOID: 1,
});
} else {
Alert.alert("Video too large please upload a shorter video");
props.navigation.goBack();
}
};
// checking camera permission
if (hasPermission === false) {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>No access to camera</Text>
</View>
);
}
// return statement
return (
<View style={styles.responsiveBox}>
<Camera
ref={cameraRef}
style={{
justifyContent: "flex-end",
alignItems: "center",
flex: 1,
width: "100%",
}}
>
{video && (
<TouchableOpacity
onPress={saveVideo}
style={{
padding: 20,
width: "100%",
backgroundColor: "#fff",
}}
>
<Text style={{ textAlign: "center", color: "#000" }}>Complete</Text>
</TouchableOpacity>
)}
<TouchableOpacity
onPress={toogleRecord}
style={{
padding: 20,
width: "100%",
backgroundColor: recording ? "#ef4f84" : "#4fef97",
}}
>
<Text style={{ textAlign: "center" }}>
{recording ? "Stop" : "Record"}
</Text>
</TouchableOpacity>
</Camera>
</View>
);
};
// styles
const styles = StyleSheet.create({
responsiveBox: {
width: wp("100%"),
height: hp("100%"),
alignItems: "center",
justifyContent: "center",
},
});
export default Recordvideo;
不要采用 useRef,而是将其作为状态,例如:
const [cameraRef , SetCameraRef] = useState()
并在 Camera 组件内调用 ref,如
为了在您的制作或APK文件中进行可靠的视频录制,请考虑使用[多伦多视频制作][1],因为他们提供专业的服务、先进的技术和经验丰富的支持,以确保无缝和高质量的结果。