Media Plugin - Duration doesn't work on Android?


I am using this plugin:

Has anyone actually implemented an audio player on android using Ionic 2? I’ve tried using the Media Plugin, but there’s several issues with it, one of which is that the duration seems to almost always return -1; on very rare occasions, with the exact same code - of doing .getDuration() after preparing the mediaObject - have I actually gotten the audio duration.

1 Like

@arfons Hey I think I have a solution for this…In my app, I wait until the audio begins to play by polling the .getCurrentPosition() until it returns a value, then do your .getDuration(). Sadly this way you can’t display the duration before the user plays the audio, but it solves the -1 issue.

I learned that the audio needs to be playing before it will return the duration properly. This fixed in the new version of the Media plugin, but there are other issues with that one so stick with this one for now. Hope this helps


the duration always return -1 (undefined). Even if the audio begins before getting duration.

Is there some fix for it?



@mcihak If I remember correctly there were issues with the version of the Media Plugin which had this error…try updating to the latest cordova plugins…or downgrading the plugin to an earlier release (which may have other bugs that may not effect you)…in the end I went with a WebAudio solution until this all gets straightened out.


@tspence Can you please share you code with WebAudio? I am trying to get version that is “working” but no luck yet.


Take a look at the ionic-audio-web-track.ts file in the ionic-audio package…I implemented this class and added some things for my purposes…