Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何连续播放播放列表中的所有音乐?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何连续播放播放列表中的所有音乐?

Javascript 如何连续播放播放列表中的所有音乐?,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试播放播放列表中的音乐。需要javascript函数来动态地连续播放所有歌曲 简单播放器 <audio id="audio" preload="auto" tabindex="0" controls=""> <source type="audio/mp3" src="music/demo.mp3"> Sorry, your browser does not support HTML5 audio. </audio> 我的播放列表中有3

我正在尝试播放播放列表中的音乐。需要javascript函数来动态地连续播放所有歌曲

简单播放器

<audio id="audio" preload="auto" tabindex="0" controls="">
    <source type="audio/mp3" src="music/demo.mp3">
    Sorry, your browser does not support HTML5 audio.
</audio>

我的播放列表中有3首歌曲,但问题是它只播放前2首歌曲,播放机跳过最后一首歌曲,然后再次重复播放列表,而不播放最后一首歌曲。

在检查它是否是实际播放的最后一首歌曲之前,您正在增加当前的
索引。这样做:

audio[0].addEventListener('ended',function(e){

    if(current == len){
        current = 0;
        link = playlist.find('a')[0];
    }else{
        link = playlist.find('a')[current];    
        current++; // <-- Increment after the check
    }
    run($(link),audio[0]);
});
音频[0]。addEventListener('ended',函数(e){
如果(当前==len){
电流=0;
link=playlist.find('a')[0];
}否则{
link=playlist.find('a')[当前];

current++;//谢谢@zhertal播放列表中最后一首歌曲后,您能建议如何停止音乐吗?您可以移动最后一行(
运行($(链接),音频[0]);
)在else中,就在
current++
之后,这样当
current
变量被重置时,它就不会被调用。非常好。再次感谢@zhertalw在firefox、chrome和ie上工作,但不在opera和safari上工作。当我从播放列表中单击歌曲时,音频
标签上不会播放任何内容,但当我删除播放列表并添加音频中的单曲
标记它工作正常。@Zhertal
var audio;
var playlist;
var tracks;
var current;

init();
function init(){
    current = 0;
    audio = $('audio');
    playlist = $('#playlist');
    tracks = playlist.find('li a');
    len = tracks.length - 1;

    playlist.find('a').click(function(e){
        e.preventDefault();
        link = $(this);
        current = link.parent().index();
        run(link, audio[0]);
    });
    audio[0].addEventListener('ended',function(e){
        current++;
        if(current == len){
            current = 0;
            link = playlist.find('a')[0];
        }else{
            link = playlist.find('a')[current];    
        }
        run($(link),audio[0]);
    });
}
function run(link, player){
        player.src = link.attr('src');
        par = link.parent();
        par.addClass('active').siblings().removeClass('active');
        audio[0].load();
        audio[0].play();
}
audio[0].addEventListener('ended',function(e){

    if(current == len){
        current = 0;
        link = playlist.find('a')[0];
    }else{
        link = playlist.find('a')[current];    
        current++; // <-- Increment after the check
    }
    run($(link),audio[0]);
});