To create a React Native Expo page to capture a 45-second video of yourself, follow the steps below:
- Install
expo-camera and expo-permissions packages, as well as expo-video-thumbnails.
expo install expo-camera
expo install expo-permissions
expo install expo-video-thumbnails
- Import necessary components and hooks.
import React, { useState, useEffect } from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Image } from 'react-native';
import { Camera } from 'expo-camera';
import * as Permissions from 'expo-permissions';
import { createThumbnail } from 'expo-video-thumbnails';
- Set up the app component for recording the video.
export default function App() {
const [hasCameraPermission, setHasCameraPermission] = useState(null);
const [isRecording, setIsRecording] = useState(false);
const [cameraRef, setCameraRef] = useState(null);
const [videoUri, setVideoUri] = useState(null);
const [thumbnailUri, setThumbnailUri] = useState(null);
useEffect(() => {
getPermissions();
}, []);
const getPermissions = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
setHasCameraPermission(status === 'granted');
};
const startRecording = async () => {
setIsRecording(true);
const videoRec = await cameraRef.recordAsync({ maxDuration: 45 });
setVideoUri(videoRec.uri);
};
const stopRecording = async () => {
setIsRecording(false);
cameraRef.stopRecording();
const {uri} = await createThumbnail({source: videoUri});
setThumbnailUri(uri);
};
- Implement UI components with start, stop, retake, and save buttons.
return (
<View style={styles.container}>
{hasCameraPermission ? (
<Camera
ref={(ref) => setCameraRef(ref)}
style={styles.camera}
autoFocus="on"
>
{!isRecording && (
<Text
style={styles.infoPrompt}
>
Record a 45s video of yourself
</Text>
)}
<View style={styles.buttonContainer}>
{!isRecording ? (
<TouchableOpacity onPress={startRecording} style={styles.startButton}>
<Text style={styles.buttonText}>Start</Text>
</TouchableOpacity>
) : (
<TouchableOpacity onPress={stopRecording} style={styles.stopButton}>
<Text style={styles.buttonText}>Stop</Text>
</TouchableOpacity>
)}
{videoUri && (
<>
<TouchableOpacity onPress={() => setVideoUri(null)} style={styles.retakeButton}>
<Text style={styles.buttonText}>Retake</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => {}} style={styles.saveButton}>
<Text style={styles.buttonText}>Save</Text>
</TouchableOpacity>
</>
)}
</View>
</Camera>
) : (
<Text>No access to camera</Text>
)}
{thumbnailUri && (
<Image resizeMode="cover" source={{ uri: thumbnailUri }} style={styles.thumbnail} />
)}
</View>
);
}
- Add styling for the components.
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
},
camera: {
flex: 1,
},
infoPrompt: {
position: 'absolute',
top: '45%',
left: 0,
right: 0,
textAlign: 'center',
color: 'white',
fontSize: 20,
backgroundColor: 'rgba(0, 0, 0, 0.6)',
paddingHorizontal: 20,
paddingVertical: 10,
},
buttonContainer: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'flex-end',
padding: 20,
},
startButton: {
backgroundColor: 'green',
padding: 15,
},
stopButton: {
backgroundColor: 'red',
padding: 15,
},
retakeButton: {
backgroundColor: 'blue',
padding: 15,
},
saveButton: {
backgroundColor: 'black',
padding: 15,
},
buttonText: {
color: 'white',
fontSize: 20,
},
thumbnail: {
position: 'absolute',
bottom: 20,
right: 20,
width: 100,
height: 100,
}
});
This solution sets up an Expo page with a camera view, text info prompt, black overlay, and buttons to start, stop, retake, and save the recorded video. The recorded video can be saved to a video folder.