apache / apache/cordova-plugin-media

How to get media file duration before playing it?

Open
#254 1 comment 6 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
391
Forks
762
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the JavaScript Media API call with a downloaded audio file and compare getDuration() before playback with its value during getCurrentPosition(). Then inspect the plugin's platform implementations to determine whether duration is available before playback; done means establishing and documenting or fixing the supported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.