0
votes

Im working with angular/nativescript with the plugin 'nativescript-audio', I need record audio with best quality possible and send to an API with HttpClient. There are a few things I need to do, like converting audio to base64 and recording it with a good quality, but I do not have a good knowledge of native audio. Currently, using this plugin, I can record and play the audio in the library itself, but when sent in base 64 it arrives unrecognizable in the API.

What Im doing:

  private async _startRecord(args) {
        if (!this._recorder.hasRecordPermission()) await this._askRecordPermission(args);
        try {
            this._isRecording = true;
            await this._recorder.start(this._getRecorderOptions());
        } catch (err) {
            console.log(err);
        }
    }


    private _getRecorderOptions(): AudioRecorderOptions {
        let audioFileName = 'record'
        let audioFolder = knownFolders.currentApp().getFolder('audio');
        let recordingPath = `${audioFolder.path}/${audioFileName}.${this.getPlatformExtension()}`;
        let androidFormat, androidEncoder;

        if (platform.isAndroid) {
            androidFormat = 4;
            androidEncoder = 2;
        }

        return {
            filename: recordingPath,
            format: androidFormat,
            encoder: androidEncoder,
            metering: true,
            infoCallback: info => { console.log(JSON.stringify(info)); },
            errorCallback: err => this._isRecording = false
        }
    }

Then:

     let audioFileName = 'record';
     let audioFolder = knownFolders.currentApp().getFolder('audio');
     let file: File = audioFolder.getFile(`${audioFileName}.${this.getPlatformExtension()}`);

     let b = file.readSync();
     var javaString = new java.lang.String(b);


        var encodedString = android.util.Base64.encodeToString(
            javaString.getBytes(),
            android.util.Base64.DEFAULT
        );

 this.service.sendFile(encodedString)
            .subscribe(e => {

                console.log(e);

            }, error => {

                console.log('ERROR ////////////////////////////////////////////');
                console.log(error.status);

            })

The service:

  sendFile(fileToUpload: any): Observable<any> {

    let url: string = `myapi.com`

    let body = { "base64audioFile": fileToUpload }

    return this.http.post<any>(url, body, {
      headers: new HttpHeaders({
        // 'Accept': 'application/json',
        // 'Content-Type': 'multipart/form-data',
        // "Content-Type": "multipart/form-data"
      }),
      observe: 'response'
    });
  }

I've already tried changing the recording options in several ways, but I do not know which one is right for the best audio quality and which formats and encodings I need:


            androidFormat = 3;
            androidEncoder = 1;
            channels: 2,
            sampleRate: 96000,
            bitRate: 1536000,

The result base64 varies a lot with the type of encode I make, but so far I have not managed to get anything recognizable, just some hissing and unrecognizable noises.

1

1 Answers

0
votes

First of all, you may just pass the bytes directly to encodeToString(...) method. You don't have to create a string from bytes, it's not valid.

Also use NO_WRAP flag instead of DEFAULT.

    var encodedString = android.util.Base64.encodeToString(
        b,
        android.util.Base64.NO_WRAP
    );

Here is a Playground Sample which I wrote a while ago to test base64 encode on iOS & Android. You might have to update the file url, the one in the source gives 404 (I had just picked a random mp3 file form internet for testing).