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 { Text } from "react-native";
import ClientProvider from "./graphql/client";
import ShotsContainer from "./component/shot";
import CameraScreen from "./component/video/camera";
const App: React.FC = () => {

View File

@ -10,6 +10,7 @@ module.exports = function(api) {
"allowUndefined": true,
"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 { Camera, useCameraPermission, useCameraDevice, useCameraFormat, PhotoFile, VideoFile } from 'react-native-vision-camera'
import { RecordingButton } from './capture-button'
import { useSharedValue } from 'react-native-reanimated'
export default function CameraScreen(): React.ReactElement {
const camera = useRef<Camera>(null)
const { hasPermission, requestPermission } = useCameraPermission()
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(() => {
console.log('Camera initialized!')
@ -47,6 +55,7 @@ export default function CameraScreen(): React.ReactElement {
format={format}
onInitialized={onInitialized}
video={true}
// orientation='portrait' // TODO: figure out what this looks like to the native module
isActive={true}
/>
<RecordingButton

View File

@ -1,45 +1,99 @@
import React from 'react';
import { TouchableOpacity, StyleSheet } from 'react-native';
import React, { useCallback, useRef, useState } from 'react';
import { TouchableOpacity, StyleSheet, View } from 'react-native';
export const RecordingButton = ({ style, camera, onMediaCaptured, enabled, setIsPressingButton }) => {
const handlePressIn = () => {
setIsPressingButton(true);
// Additional functionality when the button is pressed
console.log('pressed in')
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()
},
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();
}
};
const handlePressOut = () => {
setIsPressingButton(false);
// Stop recording and handle media capture
if (camera && enabled) {
// Assuming you have a method in your camera object to capture media
const media = camera.captureMedia();
if (onMediaCaptured) {
onMediaCaptured(media);
}
}
};
return (
<TouchableOpacity
style={[styles.captureButton, style]}
onPressIn={handlePressIn}
onPressOut={handlePressOut}
disabled={!enabled}
/>
);
return (
<TouchableOpacity
style={[styles.captureButton, style]}
onPress={handlePress}
disabled={!enabled}
>
<View style={isRecording.current ? styles.recordingSquare : styles.innerCircle} />
</TouchableOpacity>
);
};
const styles = StyleSheet.create({
captureButton: {
height: 70,
width: 70,
borderRadius: 35,
backgroundColor: '#FF3B30',
justifyContent: 'center',
alignItems: 'center',
},
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',
},
});
export default RecordingButton;

View File

@ -1,61 +1,63 @@
{
"name": "railbird-rn",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "cp .env.development .env && expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"run:android": "expo run:android",
"run:ios": "expo run:ios",
"web": "expo start --web",
"lint": "eslint . --ext .js,.ts,.tsx",
"lint:fix": "eslint . --ext .ts,.tsx --fix",
"test": "jest --forceExit"
},
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|react-native-svg-charts|d3-path)/)"
]
},
"dependencies": {
"@apollo/client": "^3.8.8",
"@react-native-firebase/app": "^18.8.0",
"@react-native-firebase/auth": "^18.8.0",
"@types/react": "~18.2.14",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"babel-plugin-inline-dotenv": "^1.7.0",
"d3-path": "^3.1.0",
"d3-scale": "^1.0.6",
"eslint": "^8.56.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-native": "^4.1.0",
"expo": "~49.0.15",
"expo-build-properties": "^0.11.0",
"expo-splash-screen": "~0.20.5",
"expo-status-bar": "~1.6.0",
"graphql": "^16.8.1",
"jest": "^29.2.1",
"jest-expo": "~49.0.0",
"react": "18.2.0",
"react-native": "0.72.6",
"react-native-dotenv": "^3.4.9",
"react-native-svg": "13.9.0",
"react-native-svg-charts": "^5.4.0",
"typescript": "^5.3.3"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@testing-library/jest-native": "^5.4.3",
"@testing-library/react-native": "^12.4.3",
"@types/d3-path": "^3.0.2",
"@types/jest": "^29.5.11",
"@types/react-native-svg-charts": "^5.0.16",
"eslint-config-prettier": "^9.1.0"
},
"private": true
"name": "railbird-rn",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "cp .env.development .env && expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"run:android": "expo run:android",
"run:ios": "expo run:ios",
"web": "expo start --web",
"lint": "eslint . --ext .js,.ts,.tsx",
"lint:fix": "eslint . --ext .ts,.tsx --fix",
"test": "jest --forceExit"
},
"jest": {
"preset": "jest-expo",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|@react-native(-community)?|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg|react-native-svg-charts|d3-path)/)"
]
},
"dependencies": {
"@apollo/client": "^3.8.8",
"@react-native-firebase/app": "^18.8.0",
"@react-native-firebase/auth": "^18.8.0",
"@types/react": "~18.2.14",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"babel-plugin-inline-dotenv": "^1.7.0",
"d3-path": "^3.1.0",
"d3-scale": "^1.0.6",
"eslint": "^8.56.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-native": "^4.1.0",
"expo": "~49.0.15",
"expo-build-properties": "^0.11.0",
"expo-splash-screen": "~0.20.5",
"expo-status-bar": "~1.6.0",
"graphql": "^16.8.1",
"jest": "^29.2.1",
"jest-expo": "~49.0.0",
"react": "18.2.0",
"react-native": "0.72.6",
"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-charts": "^5.4.0",
"react-native-vision-camera": "^3.8.2",
"typescript": "^5.3.3"
},
"devDependencies": {
"@babel/core": "^7.20.0",
"@testing-library/jest-native": "^5.4.3",
"@testing-library/react-native": "^12.4.3",
"@types/d3-path": "^3.0.2",
"@types/jest": "^29.5.11",
"@types/react-native-svg-charts": "^5.0.16",
"eslint-config-prettier": "^9.1.0"
},
"private": true
}