音频播放器上显示2个十进制时间

时间:2019-06-05 00:49:21

标签: javascript audio-player

对于我的音频播放器,如何用两位小数显示两次?

现在计数器显示... 0:7、0:8、0:9、0:10、0:11

但是应该像:

0:07、0:08、0:09、0:10、0:11

,音频文件的总时间显示为:273.298866

但是应该是:

4:13


var duration = document.getElementById('fullDuration');
var currentTime = document.getElementById('currentTime');

duration.innerHTML = mytrack.duration;


mytrack.addEventListener("loadedmetadata",function(){
    var minutes = parseInt(mytrack.duration / 60);
    var seconds = parseInt(mytrack.duration % 60);
    duration.innerHTML = minutes + ':' + seconds;
})

function update(){
  if(!mytrack.ended){
    var playedMinutes = parseInt(mytrack.currentTime/60);
    var playedSeconds = parseInt(mytrack.currentTime%60);
    currentTime.innerHTML = playedMinutes + ':' + playedSeconds;

    var size = parseInt(mytrack.currentTime*barSize/mytrack.duration);
    progressBar.style.width = size + "px";

  }
  else{
    currentTime.innerHTML = "0.00";
    playButton.style.backgroundImage = 'url(file:///Users/Pier/Desktop/Play%20button.png)';
    progressBar.style.width = "0px";
    window.clearInterval(updateTime);
  }
}

1 个答案:

答案 0 :(得分:2)

String.padStart是实现此目的的现代方法,但doesn't work with IE却是这样:

currentTime.textContent = playedMinutes + ':' + playedSeconds.toString().padStart(2, '0');