[英]HTML5 audio playlist - how to play a second audio file after the first has ended?
How could we make some audio in html5 play after another one has finished? 在另一个完成后,我们怎样才能在html5播放中制作一些音频?
I have tried with jquery delay()
function but it wont work at all, is it possible using pause()
in html5 audio with timer instead ? 我尝试过使用jquery delay()
函数,但它根本不工作,是否可以使用带有计时器的html5音频中的pause()
? For example, pause('500',function(){});
例如, pause('500',function(){});
? ?
Here's a JSLinted, unobtrusive Javascript example demonstrating how to handle and use the ended
mediaevent . 这是一个JSLinted,不显眼的Javascript 示例,演示如何处理和使用已ended
mediaevent 。 In your particular situation, you would trigger playback of the second audio file in your ended
event handler. 在您的特定情况下,您将在已ended
事件处理程序中触发第二个音频文件的播放。
You can use the code below or run the test fiddle . 您可以使用下面的代码或运行测试小提琴 。
Click an item in the playlist to begin playback. 单击播放列表中的项目开始播放。 After one audio ends, the next will begin. 一个音频结束后,下一个音频将开始。
markup: (note the deliberate avoidance of whitespace between <li>
elements - this is to simplify traversing the DOM with nextSibling
.) 标记:(注意故意避免<li>
元素之间的空白 - 这是为了简化使用nextSibling
遍历DOM。)
<audio id="player"></audio>
<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>
script: 脚本:
// 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);
}
});
If this is your audio tag: 如果这是您的音频标签:
<audio id="player" src="someAudio.mp3"/>
then adding an event listener to it for the "ended" event will make it possible to change the source and play your next sound. 然后为“已结束”事件添加一个事件监听器,可以更改源并播放下一个声音。
var audio = document.getElementById("player");
audio.addEventListener("ended", function() {
audio.src = "nextAudio.mp3";
audio.play();
});
I think this answer will help you ... 我想这个答案对你有帮助......
html5 audio player - jquery toggle click play/pause? html5音频播放器 - jquery切换点击播放/暂停?
so instead of a click you should use a setTimeout, or a setInterval, which ever you feel more comfortable with to check constantly for the .paused==false
I think you might be able to check if it is finished by checking videos's length and compare it with max length, which == end of file. 因此,您应该使用setTimeout或setInterval,而不是点击,您可以使用它来更.paused==false
地检查.paused==false
我认为您可以通过检查视频的长度来检查是否已完成并比较它有最大长度,= =文件结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.