I'm trying to implement a way for users to record and send their voice recordings. I've managed to get it working (kind of), however I'm unsure as to whether or not this is the correct approach. Currently, when a user presses and hold the record button, I create a new Audio
object, initialize it and then invoke startAsync
. Here's that bit of code:
const meteringRef = React.useRef<number[]>([]);const recordingDurationRef = React.useRef(0);const recording = React.useRef<Audio.Recording | null>(new Audio.Recording());const startRecording = React.useCallback(() => { try { recording.current = new Audio.Recording(); recording.current.setOnRecordingStatusUpdate(({ durationMillis, metering, isRecording }) => { // some logic }); recording.current.setProgressUpdateInterval(50); await recording.current.prepareToRecordAsync({ isMeteringEnabled: true, android: { extension: '.mp3', outputFormat: AndroidOutputFormat.MPEG_4, audioEncoder: AndroidAudioEncoder.AAC, sampleRate: 44100, numberOfChannels: 2, bitRate: 128000, }, ios: { extension: '.mp3', outputFormat: IOSOutputFormat.MPEG4AAC, audioQuality: IOSAudioQuality.MAX, sampleRate: 44100, numberOfChannels: 2, bitRate: 128000, linearPCMBitDepth: 16, linearPCMIsBigEndian: false, linearPCMIsFloat: false, }, web: { mimeType: 'audio/webm', bitsPerSecond: 128000, }, }); await recording.current.startAsync(); } catch (err) { console.log('Failed to start recording', err); recording.current = null; }}, []);
So there are some issues with this bit. Every time the user wants to record a voice message, (which could be very frequently), I have to create a new Audio
object (new Audio.Recording()
) and then do some async stuff to prepare it.
My question is whether or not it's possible to instantiate it once and then just "empty" it out, so that I may reuse the same, prepared object?