I’m trying to record audio in a React Native project using recorderService from usePlatformService in Sendbird UIKit. The goal is to start recording, stop it, and then access the URI of the recorded audio to send it as a voice note in the chat. However, the recorderService.uri returns undefined after stopping the recording.
Here’s some sample code
// Recording start and stop logic
const startRecording = async () => {
try {
addStateListener();
await recorderService.record();
setIsRecording(true);
console.log('Recording started');
} catch (error) {
console.error('Failed to start recording:', error);
}
};
const stopRecording = async () => {
try {
await recorderService.stop();
setIsRecording(false);
console.log('Recording stopped');
} catch (error) {
console.error('Failed to stop recording:', error);
}
};
const addStateListener = () => {
return recorderService.addStateListener((state) => {
if (state === 'completed') {
const uri = recorderService.options?.uri; // Tried accessing URI here
if (uri) {
setRecordedUri(uri);
console.log('Recording completed, URI:', uri);
// send message in chat
} else {
console.warn('Recording completed but no URI available');
}
}
});
};