From a2f6d97d8165678eb92355e17e79a94f938c96bd Mon Sep 17 00:00:00 2001 From: Alan Fernandez Date: Fri, 6 Dec 2024 11:01:48 +0100 Subject: [PATCH] feat: wip - audio recorder hook --- .../FloatingButton/hooks/useAudioRecorder.ts | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/screens/HomeScreen/components/FloatingButton/hooks/useAudioRecorder.ts b/src/screens/HomeScreen/components/FloatingButton/hooks/useAudioRecorder.ts index 378deea..24745d8 100644 --- a/src/screens/HomeScreen/components/FloatingButton/hooks/useAudioRecorder.ts +++ b/src/screens/HomeScreen/components/FloatingButton/hooks/useAudioRecorder.ts @@ -1,21 +1,49 @@ import { useState } from 'react'; +import { PermissionsAndroid, Platform } from 'react-native'; +import AudioRecorderPlayer from 'react-native-audio-recorder-player'; + +const audioRecorderPlayer = new AudioRecorderPlayer(); export const useAudioRecorder = () => { - const recorder = {}; const [isRecording, setIsRecording] = useState(false); const [recordingPath, setRecordingPath] = useState(null); const startRecording = async () => { try { + // TODO: move this to permissions helper + if (Platform.OS === 'android') { + try { + const grants = await PermissionsAndroid.requestMultiple([ + PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, + PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, + PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, + ]); + + console.log('write external storage', grants); + + if ( + grants['android.permission.WRITE_EXTERNAL_STORAGE'] === + PermissionsAndroid.RESULTS.GRANTED && + grants['android.permission.READ_EXTERNAL_STORAGE'] === + PermissionsAndroid.RESULTS.GRANTED && + grants['android.permission.RECORD_AUDIO'] === + PermissionsAndroid.RESULTS.GRANTED + ) { + console.log('Permissions granted'); + } else { + console.log('All required permissions not granted'); + return; + } + } catch (err) { + console.warn(err); + return; + } + } + setIsRecording(true); const path = 'audioRecord.m4a'; - console.log(path); - /** - * TODO: - * 1. build path - * 2. record audio - * 3. get uri? - */ + const uri = await audioRecorderPlayer.startRecorder(path); + setRecordingPath(uri); } catch (error) { console.error('Failed to start recording:', error); setIsRecording(false);