我尝试使用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>
答案 0 :(得分:0)
var audioPlayer=document.getElementById('audioPlayer');
...会出错,因为该元素没有id。
答案 1 :(得分:0)
一些事情:
为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>
元素中。