I'm currently working on a project in Ionic 3 where I need to record audio and send it to the server.
record(){
if (this.platform.is('ios')) {
this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new
Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new
Date().getSeconds()+'.3gp';
this.filePath = this.file.documentsDirectory.replace(/file:\/\//g, '') +
this.fileName;
this.audio = this.media.create(this.filePath);
} else if (this.platform.is('android')) {
this.fileName = 'record'+new Date().getDate()+new Date().getMonth()+new
Date().getFullYear()+new Date().getHours()+new Date().getMinutes()+new
Date().getSeconds()+'.3gp';
this.filePath = this.file.externalDataDirectory.replace(/file:\/\//g, '')
+ this.fileName;
this.audio = this.media.create(this.filePath);
}
this.audio.startRecord();
this.recording = true;
}
Once the recording is done, my next step is to convert the audio file to base64 and send it to the server. For this conversion, I am utilizing the base64 plugin.
this.base64.encodeFile(this.filePath).then((base64File: string) => {
// let audiooo = encodeURIComponent(base64File);
this.sendVoice(encodeURIComponent(base64File));
}, (err) => {
console.log(err);
});
However, when trying to encode the file, I encounter an issue. The encoded value starts with data%3Aimage..., instead of data%3Aaudio... which prevents me from playing the audio after sending it to the server.
data%3Aimage%2F%3Bcharset%3Dutf-8%3Bbase64%2C%2f%2FFsQBIF%2FAFAC....
Do you have any insights on what might be causing this problem?