I am trying to utilize capacitor-voice-record library to:
- Record audio
- Play the audio locally before sending data to the server
- Send audio file to the server
Here’s the current code:
In angular service
startRecording = ()=> {
try {
return VoiceRecorder.requestAudioRecordingPermission().then((result: GenericResponse) => {
if (result.value) {
return VoiceRecorder.startRecording()
} else {
return Promise.reject('Permission denied');
}
})
} catch (error) {
console.log(error);
return Promise.reject(error);
}
}
stopRecording = async () => {
console.log("stopRecording");
let result = await VoiceRecorder.stopRecording();
let blob = base64StringToBlob(result.value.recordDataBase64, result.value.mimeType);
let data_text = await blob.text();
let extension = mime.getExtension(result.value.mimeType);
let write_result = await Filesystem.writeFile({
path: `temp.${extension}`,
data: data_text,
directory: Directory.External,
encoding: Encoding.UTF8,
});
let uri = write_result.uri;
return uri;
}
At this point I tested the locally stored file. Sent the file to PC and tried to playback the audio but audio player gives corrupted or codec error (note tried multiple audio players). While .aac file downloaded from the internet works absolutely fine.
What have I tried till now:
- Tried storing data as shown above
- Tried forcing the extension to be
.aac
instead of.adts
thatmime.getExtension
suggests - Tried storing
base64
data directly - Tried storing
data:${result.value.mimeType};base64,${result.value.recordDataBase64}
None of these have worked. I think the issue maybe with the audio encoding but its just a guess.
Any suggestions would be appreciated.