我正在尝试创建一种播放列表功能,可以使用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>发布于 2010-11-07 04:40:07
以下是一些事情:
给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-07 04:31:59
var audioPlayer=document.getElementById('audioPlayer');…将会出错,因为元素没有id。
https://stackoverflow.com/questions/4114977
复制相似问题