tchvu3 / capacitor-voice-recorder

Capacitor plugin for voice recording
69 stars 57 forks source link

[Help] Data stored in audio file doesn't playback #46

Open shyamal890 opened 2 months ago

shyamal890 commented 2 months ago

I am trying to utilize this library to:

  1. Record audio
  2. Play the audio locally before sending data to the server
  3. Send audio file to the server

Here's the current code:

In 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 temp.aac. 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:

  1. Tried storing data as shown above
  2. Tried forcing the extension to be .aac instead of .adts that mime.getExtension suggests
  3. Tried storing base64 data directly
  4. Tried storing data:${result.value.mimeType};base64,${result.value.recordDataBase64}

None of these have worked. Any suggestions would be appreciated.

PS: Maybe we need to encode the raw data before storing it to a file. Ref - https://stackoverflow.com/a/18970406/3955513