使用javascript在特定时间(ogg / mp3)播放音频标签

时间:2014-07-01 11:14:48

标签: javascript html5 audio

使用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>

1 个答案:

答案 0 :(得分:1)

只有在meta data可用时才能查找给定时间:

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

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