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.