标题: 探索React Native屏幕录制的奥秘 —— react-native-record-screen详解
import { useRef, useEffect } from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import RNRecordScreen from 'react-native-record-screen';
const ScreenRecorderButton = () => {
const recorderRef = useRef(null);
const startRecording = async () => {
try {
const isAvailable = await RNRecordScreen.isAvailable();
if (isAvailable) {
await RNRecordScreen.startRecording();
console.log('录制开始');
} else {
console.log('屏幕录制功能不可用');
}
} catch (error) {
console.error('录制开始失败:', error);
}
};
const stopRecording = async () => {
try {
const result = await RNRecordScreen.stopRecording();
console.log('录制结束:', result);
} catch (error) {
console.error('录制结束失败:', error);
}
};
useEffect(() => {
// 可以在这里添加更多的初始化逻辑
}, []);
return (
<View>
<TouchableOpacity onPress={startRecording}>
<Text>开始录制屏幕</Text>
</TouchableOpacity>
<TouchableOpacity onPress={stopRecording}>
<Text>停止录制屏幕</Text>
</TouchableOpacity>
</View>
);
};
export default ScreenRecorderButton;
这段代码使用React Native和react-native-record-screen
库提供了一个简单的屏幕录制功能。用户可以通过点击按钮来开始和停止录制。代码中包含了基本的错误处理,以确保在录制过程中遇到问题时能够输出错误信息。
评论已关闭