1

我正在尝试使用 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>
4

2 回答 2

0
var audioPlayer=document.getElementById('audioPlayer');

… 会出错,因为元素没有 id。

于 2010-11-06T20:31:59.347 回答
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>元素中。

于 2010-11-06T20:40:06.767 回答