Javascript在某一点播放音频

时间:2014-10-03 19:21:12

标签: javascript html audio

我正在创建一个网页,其中包含通过HTML音频标记实现的音频文件。音频目前由观众控制,但是,我希望有一个按钮(HTML或Javascript),当点击时,它将开始播放特定时间点的音频。 我怎样才能做到这一点? 感谢

2 个答案:

答案 0 :(得分:4)

我从文档中的示例中获得了以下答案: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video

var mediaElement = document.getElementById('mediaElementID');
mediaElement.currentTime = 122; //Skips to 122 seconds into the song

只需点击一个按钮并调用上面的脚本,它就会跳到你指定的秒数指定的歌曲中的点。

答案 1 :(得分:0)

在同一页面上,您可以开发类似的内容:

<html>
<audio id="demo" src="The Runaways - Cherry Bomb.mp3"></audio>
<div>
  <button onclick="document.getElementById('demo').play()">Play the Audio</button>
  <button onclick="document.getElementById('demo').pause()">Pause the Audio</button>
  <button onclick="document.getElementById('demo').volume+=0.1">Increase Volume</button>
  <button onclick="document.getElementById('demo').volume-=0.1">Decrease Volume</button>
  Jump to <input id="time" type="text" value="30"> (in seconds)</input>
  <button onclick="document.getElementById('demo').currentTime=document.getElementById('time').value">Jump</button>
</div>
</html>