我正在使用位于我的WP网站的<head>
部分的javascript链接。它非常简单并且效果很好,除非玩家结束最后一首曲目 - 它再次开始播放第一首曲目。
我试图使用loop="false"
,但它启动另一个错误,重新启动相同的最后一个轨道。我不熟悉编码javascript,但我认为应该可以创建一个类,像class="last-song"
这样的东西。
在here中可以看到一个例子,可以检查HTML:
javascript可以找到here
如果有人能得到一些帮助,我将非常感激。
你的代码
$("#audioPlayer")[0].addEventListener("ended", function(){
currentSong++;
if(currentSong == $("#playlist li a").length)
currentSong = 0;
$("#playlist li").removeClass("current-song");
$("#playlist li:eq("+currentSong+")").addClass("current-song");
$("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
$("#audioPlayer")[0].play();
});
}
当你在currentSong = 0;
语句之后设置if(currentSong...
时,你告诉它循环。基本上,每当它到达终点时,你就会将它重置回阵列中的第一首歌曲。
如果您将其更改为:
if (currentSong == $("#playlist li a").length) {
$("#audioPlayer")[0].pause() // maybe .stop()
// Do more stuff here for your UI
}
看起来你也可以什么也不做(只是注释掉currentSong = 0
线)。例如:
$("#audioPlayer")[0].addEventListener("ended", function(){
currentSong++;
if(currentSong == $("#playlist li a").length)
//currentSong = 0;
$("#playlist li").removeClass("current-song");
$("#playlist li:eq("+currentSong+")").addClass("current-song");
$("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
$("#audioPlayer")[0].play();
});
$("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
$("#audioPlayer")[0].play();
这就是说在数组序列中播放第一个
我对if语句感到困惑,因为它似乎没有做任何事情
if(currentSong == $("#playlist li a").length)
因为没有花括号{}来指定它在IF和ELSE上的作用
function audioPlayer(){
var currentSong = 0;
$("#audioPlayer")[0].src = $("#playlist li a")[0];
$("#audioPlayer")[0].play();
$("#playlist li a").click(function(e){
e.preventDefault();
$("#audioPlayer")[0].src = this;
$("#audioPlayer")[0].play();
$("#playlist li").removeClass("current-song");
currentSong = $(this).parent().index();
$(this).parent().addClass("current-song");
});
$("#audioPlayer")[0].addEventListener("ended", function(){
currentSong++;
if(currentSong == $("#playlist li a").length){
currentSong = 0;
$("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
$("#audioPlayer")[0].pause();
}else{
$("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
$("#audioPlayer")[0].play();
}
$("#playlist li").removeClass("current-song");
$("#playlist li:eq("+currentSong+")").addClass("current-song");
}
);
}