2024-01-30 01:15:23 -07:00
|
|
|
import React, { useCallback, useRef, useState } from 'react';
|
2024-01-30 18:05:43 -07:00
|
|
|
import { TouchableOpacity, StyleSheet, View, StyleProp, ViewStyle } from 'react-native';
|
2024-01-30 13:55:52 -07:00
|
|
|
import { CameraRoll } from "@react-native-camera-roll/camera-roll";
|
2024-01-30 18:05:43 -07:00
|
|
|
import { Camera } from 'react-native-vision-camera/lib/typescript/Camera';
|
|
|
|
import { VideoFile } from 'react-native-vision-camera/lib/typescript/VideoFile';
|
2024-01-30 13:55:52 -07:00
|
|
|
|
2024-01-30 18:05:43 -07:00
|
|
|
interface RecordingButtonProps {
|
|
|
|
style: StyleProp<ViewStyle>;
|
|
|
|
camera: React.RefObject<Camera>;
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
onMediaCaptured: (media: VideoFile, mediaType: string) => void;
|
|
|
|
enabled: boolean;
|
|
|
|
}
|
2024-01-29 20:01:45 -07:00
|
|
|
|
|
|
|
|
2024-01-30 18:05:43 -07:00
|
|
|
export const RecordingButton: React.FC<RecordingButtonProps> = ({ style, camera, onMediaCaptured, enabled }) => {
|
2024-01-30 01:15:23 -07:00
|
|
|
const isRecording = useRef(false)
|
|
|
|
// UseRef won't trigger a re-render
|
|
|
|
const [, setRecordingState] = useState(false);
|
|
|
|
|
|
|
|
const onStoppedRecording = useCallback(() => {
|
|
|
|
isRecording.current = false
|
|
|
|
setRecordingState(false)
|
|
|
|
console.log('stopped recording video!')
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const stopRecording = useCallback(async () => {
|
|
|
|
try {
|
|
|
|
if (camera.current === null) {
|
|
|
|
throw new Error('Camera ref is null!') // Error handling could be more graceful
|
|
|
|
}
|
|
|
|
console.log('calling stopRecording()...')
|
|
|
|
await camera.current.stopRecording()
|
|
|
|
console.log('called stopRecording()!')
|
|
|
|
} catch (e) {
|
|
|
|
console.error('failed to stop recording!', e)
|
|
|
|
}
|
|
|
|
}, [camera])
|
|
|
|
|
|
|
|
const startRecording = useCallback(() => {
|
|
|
|
try {
|
|
|
|
if (camera.current === null) {
|
|
|
|
throw new Error('Camera ref is null!') // Error handling could be more graceful
|
|
|
|
}
|
|
|
|
console.log('calling startRecording()...')
|
|
|
|
camera.current.startRecording({
|
|
|
|
onRecordingError: (error) => {
|
|
|
|
console.error('Recording failed!', error)
|
|
|
|
onStoppedRecording()
|
|
|
|
},
|
2024-01-30 13:55:52 -07:00
|
|
|
onRecordingFinished: async (video) => {
|
2024-01-30 01:15:23 -07:00
|
|
|
onMediaCaptured(video, 'video')
|
2024-01-30 13:55:52 -07:00
|
|
|
const path = video.path
|
|
|
|
await CameraRoll.saveAsset(`file://${path}`, {
|
|
|
|
type: 'video',
|
|
|
|
})
|
2024-01-30 01:15:23 -07:00
|
|
|
onStoppedRecording()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
console.log('called startRecording()!')
|
|
|
|
isRecording.current = true
|
|
|
|
setRecordingState(true)
|
|
|
|
} catch (e) {
|
|
|
|
console.error('failed to start recording!', e, 'camera')
|
|
|
|
}
|
|
|
|
}, [camera, onMediaCaptured, onStoppedRecording])
|
2024-01-29 20:01:45 -07:00
|
|
|
|
2024-01-30 01:15:23 -07:00
|
|
|
const handlePress = () => {
|
|
|
|
if (isRecording.current) {
|
|
|
|
stopRecording();
|
|
|
|
} else {
|
|
|
|
startRecording();
|
|
|
|
}
|
2024-01-29 20:01:45 -07:00
|
|
|
};
|
|
|
|
|
2024-01-30 01:15:23 -07:00
|
|
|
return (
|
|
|
|
<TouchableOpacity
|
|
|
|
style={[styles.captureButton, style]}
|
|
|
|
onPress={handlePress}
|
|
|
|
disabled={!enabled}
|
|
|
|
>
|
|
|
|
<View style={isRecording.current ? styles.recordingSquare : styles.innerCircle} />
|
|
|
|
</TouchableOpacity>
|
|
|
|
);
|
2024-01-29 20:01:45 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2024-01-30 01:15:23 -07:00
|
|
|
captureButton: {
|
|
|
|
height: 80,
|
|
|
|
width: 80,
|
|
|
|
borderRadius: 40,
|
|
|
|
borderWidth: 3,
|
|
|
|
borderColor: 'white',
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
|
|
|
innerCircle: {
|
|
|
|
height: 70,
|
|
|
|
width: 70,
|
|
|
|
borderRadius: 35,
|
|
|
|
backgroundColor: '#FF3B30',
|
|
|
|
},
|
|
|
|
recordingSquare: {
|
|
|
|
height: 40,
|
|
|
|
width: 40,
|
|
|
|
borderRadius: 10,
|
|
|
|
backgroundColor: '#FF3B30',
|
|
|
|
},
|
2024-01-29 20:01:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
export default RecordingButton;
|