使用javascript在特定时间(mp3)加载音频标签。
当前代码如下,完成将忽略currentTime。
我的目标是根据计算机硬件时钟的时间(秒)自动播放mp3文件。

<!DOCTYPE html>
<html>
<body>

<button onclick="setCurTime()" type="button">Set time position to 5 seconds</button>
<br>


<audio id="audio1"  controls="controls">
  <source src="http://attackbeat.com/audio/Masayume%20Chasing.mp3" type="audio/mp3">

  Your browser does not support HTML5 video.
</audio>

<script>
   var time = new Date();
 var timex = time.getSeconds();
myAudio=document.getElementById("audio1");

window.onload = function () {

myAudio.play();
myAudio.currentTime=10;
}
function setCurTime()
  {

  myAudio.currentTime=timex;
myAudio.play();
  }
</script>
</body>
</html>

最佳答案

仅当meta data可用时,您才能寻求给定时间:

var audio = document.getElementById('audio1');

audio.onloadedmetadata = function() {
  audio.currentTime = new Date().getSeconds(); // autoplay based on current time (seconds)
  audio.play();
};

10-02 14:05