我有这个代码,我有三个音频...我想在页面完全加载后一个接一个地播放它...这发生在以下代码但我似乎无法解决延迟两个音频之间...我如何实现它?
<audio id="my_audio">
<source src="bark.mp3" type="audio/mpeg">
</audio>
<audio id="my_audio2">
<source src="roar.mp3" type="audio/mpeg">
</audio>
<audio id="my_audio3">
<source src="hoot.mp3" type="audio/mpeg">
</audio>
<script type="text/javascript">
$(document).ready(function() {
$("#my_audio").get(0).play();
$("#my_audio").bind("ended", function(){
$("#my_audio2").get(0).play();
$("#my_audio2").bind("ended", function(){
$("#my_audio3").get(0).play();
});
});
});
答案 0 :(得分:1)
这是一个JSLinted,不显眼的Javascript示例,演示如何处理和使用已结束的https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events。在您的特定情况下,您将在已结束的事件处理程序中触发第二个音频文件的播放。
http://jsfiddle.net/75lb/jwyMH/
<ul id="playlist"><li data-ogg="http://www.lunerouge.org/sons/sf/LRWeird%201%20by%20Lionel%20Allorge.ogg">Space 1</li><li data-ogg="http://www.lunerouge.org/sons/sf/LRWeird%202%20by%20Lionel%20Allorge.ogg">Space 2</li><li data-ogg="http://www.lunerouge.org/sons/sf/LRWeird%203%20by%20Lionel%20Allorge.ogg">Space Lab</li></ul>
<button id="stop">Stop</button>
// globals
var _player = document.getElementById("player"),
_playlist = document.getElementById("playlist"),
_stop = document.getElementById("stop");
// functions
function playlistItemClick(clickedElement) {
var selected = _playlist.querySelector(".selected");
if (selected) {
selected.classList.remove("selected");
}
clickedElement.classList.add("selected");
_player.src = clickedElement.getAttribute("data-ogg");
_player.play();
}
function playNext() {
var selected = _playlist.querySelector("li.selected");
if (selected && selected.nextSibling) {
playlistItemClick(selected.nextSibling);
}
}
// event listeners
_stop.addEventListener("click", function () {
_player.pause();
});
_player.addEventListener("ended", playNext);
_playlist.addEventListener("click", function (e) {
if (e.target && e.target.nodeName === "LI") {
playlistItemClick(e.target);
}
});
如果这是您的音频标签:
<audio id="player" src="someAudio.mp3"/>
然后为“已结束”事件添加一个事件监听器,可以更改源并播放下一个声音。
var audio = document.getElementById("player");
audio.addEventListener("ended", function() {
setTimeout(function(){
//HERE IS YOUR CODE DO WHAT YOU WANT
audio.src = "nextAudio.mp3";
audio.play();
},5000)// 5 sec delay before your nrxt function call
});