I’m trying to use the native media plugin on ionic 2 to record and play media, but there are several issues…First, my code:
First thing, I create a directory with the file plugin…
this.File.resolveDirectoryUrl(cordova.file.dataDirectory).then((result) => {
this.File.getDirectory(result,"temp_audio",{create: true}).then((tempDir) => {
}, (error) => {});
}, (error) => {});
And then I made a function to begin recording, that first creates a file in a temp directory and then begins recording to it…
record(audio_number: number): Promise<MediaObject>{
let path = cordova.file.dataDirectory.toString() + "temp_audio/";
let filename = "L"+ audio_number +".3gp";
let full_path = path + filename;
return this.File.createFile(path,filename,true).then((audio_file) => {
return this.media.create(full_path).then((mediaObj) => {
mediaObj.startRecord();
return mediaObj;
});
});
}
And then afterwards this function is called to finish the recording.
finishRecord(mediaObj: MediaObject, audio_number: number){
let path = cordova.file.dataDirectory.toString() + "temp_audio/";
let filename = "L"+ audio_number +".3gp";
let full_path = path + filename;
mediaObj.stopRecord();
return Promise.resolve(full_path);
}
I do so by having a button that starts recording and creates an alert; dismissing the alert will send the record function’s mediaObj return parameter as an argument for finishRecord, like so:
recordAudio(){
this.audioService.record(this.audio_no).then((mediaObj) => {
let btnStop = {
cssClass: 'alertButtonDanger',
text: this.translate.instant('record.stop'),
role: 'cancel',
handler: () => {
this.finishRecording(mediaObj);
}
};
let alert = this.alertCtrl.create({
title: this.translate.instant("record.recording"),
buttons: [btnStop]
});
alert.present();
});
}
But…what’s actually going on is that on my phone’s root I get a tmprecording.3gp file which keeps recording on and on until I close the app.