working on emulator but crashing on physical device after calling startRecording

This commit is contained in:
Loewy 2024-01-30 00:15:23 -08:00
parent eb152bc6c7
commit feafd48443
5 changed files with 160 additions and 97 deletions

View File

@ -1,7 +1,4 @@
import React from "react"; import React from "react";
import { Text } from "react-native";
import ClientProvider from "./graphql/client";
import ShotsContainer from "./component/shot";
import CameraScreen from "./component/video/camera"; import CameraScreen from "./component/video/camera";
const App: React.FC = () => { const App: React.FC = () => {

View File

@ -10,6 +10,7 @@ module.exports = function(api) {
"allowUndefined": true, "allowUndefined": true,
"verbose": false, "verbose": false,
}], }],
'react-native-reanimated/plugin',
], ],
}; };
}; };

View File

@ -2,12 +2,20 @@ import React, { useCallback, useRef, useState } from 'react'
import { StyleSheet, Text, View } from 'react-native' import { StyleSheet, Text, View } from 'react-native'
import { Camera, useCameraPermission, useCameraDevice, useCameraFormat, PhotoFile, VideoFile } from 'react-native-vision-camera' import { Camera, useCameraPermission, useCameraDevice, useCameraFormat, PhotoFile, VideoFile } from 'react-native-vision-camera'
import { RecordingButton } from './capture-button' import { RecordingButton } from './capture-button'
import { useSharedValue } from 'react-native-reanimated'
export default function CameraScreen(): React.ReactElement { export default function CameraScreen(): React.ReactElement {
const camera = useRef<Camera>(null) const camera = useRef<Camera>(null)
const { hasPermission, requestPermission } = useCameraPermission() const { hasPermission, requestPermission } = useCameraPermission()
const [isCameraInitialized, setIsCameraInitialized] = useState(false) const [isCameraInitialized, setIsCameraInitialized] = useState(false)
const [isPressingButton, setIsPressingButton] = useState(false) const isPressingButton = useSharedValue(false)
const setIsPressingButton = useCallback(
(_isPressingButton: boolean) => {
isPressingButton.value = _isPressingButton
},
[isPressingButton],
)
const onInitialized = useCallback(() => { const onInitialized = useCallback(() => {
console.log('Camera initialized!') console.log('Camera initialized!')
@ -47,6 +55,7 @@ export default function CameraScreen(): React.ReactElement {
format={format} format={format}
onInitialized={onInitialized} onInitialized={onInitialized}
video={true} video={true}
// orientation='portrait' // TODO: figure out what this looks like to the native module
isActive={true} isActive={true}
/> />
<RecordingButton <RecordingButton

View File

@ -1,44 +1,98 @@
import React from 'react'; import React, { useCallback, useRef, useState } from 'react';
import { TouchableOpacity, StyleSheet } from 'react-native'; import { TouchableOpacity, StyleSheet, View } from 'react-native';
export const RecordingButton = ({ style, camera, onMediaCaptured, enabled, setIsPressingButton }) => { export const RecordingButton = ({ style, camera, onMediaCaptured, enabled, setIsPressingButton }) => {
const handlePressIn = () => { const isRecording = useRef(false)
setIsPressingButton(true); // UseRef won't trigger a re-render
// Additional functionality when the button is pressed const [, setRecordingState] = useState(false);
console.log('pressed in')
};
const handlePressOut = () => { const onStoppedRecording = useCallback(() => {
setIsPressingButton(false); isRecording.current = false
// Stop recording and handle media capture setRecordingState(false)
if (camera && enabled) { console.log('stopped recording video!')
// Assuming you have a method in your camera object to capture media }, [])
const media = camera.captureMedia();
if (onMediaCaptured) { const stopRecording = useCallback(async () => {
onMediaCaptured(media); 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()
},
onRecordingFinished: (video) => {
console.log(`Recording successfully finished! ${video.path}`)
onMediaCaptured(video, 'video')
onStoppedRecording()
},
})
// TODO: wait until startRecording returns to actually find out if the recording has successfully started
console.log('called startRecording()!')
isRecording.current = true
setRecordingState(true)
} catch (e) {
console.error('failed to start recording!', e, 'camera')
}
}, [camera, onMediaCaptured, onStoppedRecording])
const handlePress = () => {
if (isRecording.current) {
stopRecording();
} else {
startRecording();
} }
}; };
return ( return (
<TouchableOpacity <TouchableOpacity
style={[styles.captureButton, style]} style={[styles.captureButton, style]}
onPressIn={handlePressIn} onPress={handlePress}
onPressOut={handlePressOut}
disabled={!enabled} disabled={!enabled}
/> >
<View style={isRecording.current ? styles.recordingSquare : styles.innerCircle} />
</TouchableOpacity>
); );
}; };
const styles = StyleSheet.create({ const styles = StyleSheet.create({
captureButton: { captureButton: {
height: 80,
width: 80,
borderRadius: 40,
borderWidth: 3,
borderColor: 'white',
backgroundColor: 'transparent',
justifyContent: 'center',
alignItems: 'center',
},
innerCircle: {
height: 70, height: 70,
width: 70, width: 70,
borderRadius: 35, borderRadius: 35,
backgroundColor: '#FF3B30', backgroundColor: '#FF3B30',
justifyContent: 'center', },
alignItems: 'center', recordingSquare: {
height: 40,
width: 40,
borderRadius: 10,
backgroundColor: '#FF3B30',
}, },
}); });

View File

@ -43,8 +43,11 @@
"react": "18.2.0", "react": "18.2.0",
"react-native": "0.72.6", "react-native": "0.72.6",
"react-native-dotenv": "^3.4.9", "react-native-dotenv": "^3.4.9",
"react-native-reanimated": "^3.6.2",
"react-native-static-safe-area-insets": "^2.2.0",
"react-native-svg": "13.9.0", "react-native-svg": "13.9.0",
"react-native-svg-charts": "^5.4.0", "react-native-svg-charts": "^5.4.0",
"react-native-vision-camera": "^3.8.2",
"typescript": "^5.3.3" "typescript": "^5.3.3"
}, },
"devDependencies": { "devDependencies": {
@ -58,4 +61,3 @@
}, },
"private": true "private": true
} }