[英]New to Javascript, need help with HTML5 audio “playlist”
我试图创建一种播放列表功能,以结合使用HTML5和javascript在iPhone上运行。 我是该平台的新手,我希望这里有人可以帮助我确定代码问题。 第一首歌曲可以正确自动播放,但是当第二首歌曲结束时,则不会加载和播放。 预先提供了我网站的javascript代码,谢谢。 (我很抱歉,如果这个代码被搞砸了,我从iv中学到的东西以及可以在网上找到其他地方的东西中总结出来)
<script type="text/javascript">
var songname="Bottoms Up";
var counter=1;
var totalsongs=3;
while (counter<=totalsongs-1) {
document.write(<h2>songname</h2>);
switch (counter) {
case 1:
var nextSong="audio/Hey.mp3";
var audioPlayer=document.getElementById('audioPlayer');
audioPlayer.onend = function() {
audioPlayer.src = nextSong;
songname="Hey";
counter=(counter+1);
if(counter>totalsongs-1) {
counter=1;
} //if close
} //onend function close
case 2:
var nextSong="audio/I Like It.mp3";
var audioPlayer=document.getElementById('audioPlayer');
audioPlayer.onend = function() {
audioPlayer.src = nextSong;
songname="I Like It";
counter=(counter+1);
if(counter>totalsongs-1) {
counter=1;
} //if close
} //onend function close
} //switch close
} //while close
</script>
<center><audio src="audio/Bottoms Up.mp3" autoplay controls /></center>
var audioPlayer=document.getElementById('audioPlayer');
…将出错,因为该元素没有ID。
一些东西:
给audio
元素一个ID:
<audio src="audio/Bottoms Up.mp3" autoplay controls id="audioPlayer" /></center>
使用数组而不是许多变量:
var songs=({{"title": "First title","filename":"firstfile.mp3"},
{"title": "Second title","filename":"secondfile.mp3"}});
收听事件ended
如下所示:
document.getElementById("audioPlayer").addEventListener("ended", nextSong, false);
并将脚本放在<head>
元素中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.