Obtaining the entire playback time of the media plug-in 1 file in cordova

Asked 2 years ago, Updated 2 years ago, 59 views

I am trying to create something that can play music using the media plug-in of cordova, but can I get the entire playback time of one file in the media plug-in?
 I would like to use range to show what percentage of 100% is currently being played.

monaca cordova

2022-09-30 20:50

2 Answers

The media plug-in uses the following methods to determine the overall time.

media.getDuration()

References
http://docs.monaca.mobi/cur/ja/reference/phonegap_34/ja/media/


2022-09-30 20:50

It also says user7461, but

During a performance, the current position (time) can be obtained in media.getCurrentPosition().
The overall length (performance time for the entire track) can be obtained by media.getDuration().

However, as far as I checked on Android environment, even if I called getDuration() immediately after new Media(), -1 was returned, and when I ran play() in getCurrentPosition() callback, the correct value was returned (I don't know if it's just my environment or specification).

If I were to display what percentage, would it be like this?

verticalTime = null;
varmedia = new Media(url, onSuccess, onError);

// If I say media.getDuration() here, does it return -1?

media.play();

setInterval(function(){
    media.getCurrentPosition(function(position){
        if(totalTime==null)
        {
            totalTime=media.getDuration();
        }
        varelapped=(100*position/totalTime).toFixed(1)+"%";
        document.getElementById('elapsed').innerHTML =elapsed;
    }, function(e){
        console.log("Error getting pos="+e);
        document.getElementById('elapped').innerHTML="Error:"+e;
    });
}, 1000);


2022-09-30 20:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.