I am trying to save an audio and send it to the server in ionic 3.
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;
}
After stopping the record i want to send the audio file in base64 to the server. so I am using the base64 plugin to convert the audio file to base64.
this.base64.encodeFile(this.filePath).then((base64File: string) => {
// let audiooo = encodeURIComponent(base64File);
this.sendVoice(encodeURIComponent(base64File));
}, (err) => {
console.log(err);
});
However, using this plugin i get base64File with the below value:
data%3Aimage%2F%3Bcharset%3Dutf-8%3Bbase64%2C%2f%2FFsQBIF%2FAFAC....
I cannot play this encoded audio after sending it to the server, i believe it is because the file starts data%3Aimage... not with data%3Aaudio...
So any idea what am i doing wrong?