[MediaPlugin] How to know the aduio playback is done?


#1

Hi,

In my app, user clicks the button to play an audio file (a 3gp file, in my android tablet).
I’d like to disable the button during the playback and enable it again when done.

I considered to get the file duration like below, then getCurrentPosition() to compare the two value.
However, the duration value is -1. What can I do to achieve the desire?

// get file duration
let audioObject: MediaObject = this.media.create(fileURI);
let duration = audioObject.getDuration();
console.log(duration);

Thanks,
Danielle


#2

Can you use the onSuccess callback that can be passed at the time of create()?


#3

I tried to pass the two callbacks as below, but it works oddly.
Console prints ‘success’ twice when I clicked the playButton, and prints ‘err’ when audio is done.
Maybe because it doesn’t parse .3gp file content correctly?

I will share more info when I get time to check this part days later.

p.s. docs of many ionic native plugins need drastic updates, cordova docs are more useful :).

clickPlay() {
 let audioObject: MediaObject = this.media.create(fileURI, () => console.log('success');, (err) => console.log('err: ' + err); }
 audioObject.play();
}

#4

Let us know which ones or better edit the pages yourself and submit Pull Requests with the changes. Ionic Native is a 99% community maintained project.


#5

I will when I hit them (again)


#6

P.P.S. If you had read the ionic-native docs instead of the cordova ones, you would have gotten the arguments correct, instead of mistakenly believing the first one was success. Instead it is status update.


#7

Yup, the native plugins (officially supported by Ionic team), works better. Check the list, and how they work there:

You still need to install them inside your Ionic project, but everything in this list works better than a cordova standard plugin, patched to your project.

Hope this helps :slight_smile: