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