0

我正在用 javascript 制作自己的音乐播放器,并且正在制作下一首歌曲按钮。我将其设置为将歌曲添加到播放列表的位置,并将其相应的 ID 号存储在数组中。然后我索引数组以查找歌曲的当前 ID,然后当用户点击下一个时,它会转到数组中的下一首歌曲。

这是我的代码:

    $(document).ready(function(){
    $("#player").prop("volume",".5");
});

    var xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","playlist.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    var songlist = new Array();
    var currentSong;

function song_play(song_id){
    var player = document.getElementById("player");
    player.setAttribute("src", "Music/"+song_id+".mp3");
    player.play();

    songlist.push(song_id);
    alert(JSON.stringify(songlist));
    currentSong = song_id;

            var new_id=song_id-1;
            $("#marquee").empty();
            $("#marquee").append("<span style='color:red;'>Now Playing: </span>"+xmlDoc.getElementsByTagName("artist")[new_id].childNodes[0].nodeValue+"-");
            $("#marquee").append(xmlDoc.getElementsByTagName("track")[new_id].childNodes[0].nodeValue);

};

function add_song(song_id,new_id){
    var artist = xmlDoc.getElementsByTagName("artist")[new_id].childNodes[0].nodeValue;
    var track = xmlDoc.getElementsByTagName("track")[new_id].childNodes[0].nodeValue;
    $("#song_list").append("<a id="+song_id+" href='javascript:void(0)' onclick='song_play(this.id);' class='song'>"+artist+"-"+track+"</a><br/>");
};

function nextSong(currentSong){
        var currentSongIndex = songlist.indexOf(currentSong);
        var newSong = songlist[currentSongIndex + 1];
        song_play(newSong);
};

我遇到的问题是,一旦我按下下一个按钮,它就会停止一起播放音乐。

4

1 回答 1

1

newSong 表示歌曲对象,而不是歌曲的 id,您的 song_play 方法实际上是使用歌曲 id。将 nextSong 函数更改为:

function nextSong(currentSong){
    var currentSongIndex = songlist.indexOf(currentSong);
    song_play(currentSongIndex + 1);
};
于 2013-10-29T05:07:43.117 回答