Skip to content

Commit

Permalink
feat: wip - audio recorder hook
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanFnz committed Dec 6, 2024
1 parent c4cf468 commit a2f6d97
Showing 1 changed file with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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<string | null>(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);
Expand Down

0 comments on commit a2f6d97

Please sign in to comment.