I would like to display the current time of of an html 5 audio element and the duration of that element. I\'ve been looking over the internet but can\'t find a functional sc
When you use audio.duration(). It will give you result in seconds. You just have to change this in minutes and seconds. Demo of Code is here, hope it will help you.
song.addEventListener('timeupdate',function (){
var duration = song.duration; //song is object of audio. song= new Audio();
var sec= new Number();
var min= new Number();
sec = Math.floor( duration );
min = Math.floor( sec / 60 );
min = min >= 10 ? min : '0' + min;
sec = Math.floor( sec % 60 );
sec = sec >= 10 ? sec : '0' + sec;
$("#total_duration").html(min + ":"+ sec); //Id where i have to print the total duration of song.
});
This code will definitely work for total duration. To get current Time, you just have to use song.currentTime; in place of song.duration;
Whole code will remain same.