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 に答える