cordova-plugin-media icon indicating copy to clipboard operation
cordova-plugin-media copied to clipboard

How to get media file duration before playing it?

Open rushabhmakwana-multidots opened this issue 6 years ago • 1 comments

I am developing an ionic app in which I need to display media duration immediately after downloading it. I tried to show it with the below code:

let audio = this.media.create('XXXX');
let audioDuration = audio.getDuration(); // it returns me '-1'.

But when I play this audio and trying to get the audio duration in getCurrentPosition() function it gives the audio duration. For this, my code is below:

let audio = this.media.create('XXXX');
audio.play(); 

this.audio.getCurrentPosition(
// success callback
  (position) => {
    if (position > -1) {
      var seconds = this.audio.getDuration(); // returns no.of seconds.
   }
 },
  // error callback
  (e) => {
    console.log("Error getting pos=" + e);
  }
);

How can I get the audio duration before playing it? Please help.

Suggest to use audio.seekTo(1) then call audio.getDuration() with 500ms delay. Checking on this thread for more info. https://stackoverflow.com/a/61358243/3967044

nkmkevin avatar Apr 27 '20 09:04 nkmkevin