html5 display audio currentTime

前端 未结 9 921
生来不讨喜
生来不讨喜 2020-11-28 05:29

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

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 06:10

    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.

提交回复
热议问题