简体   繁体   English

jPlayer with playlist: 'ended' 仅在下一曲目的 'setmedia' 之后触发

[英]jPlayer with playlist: 'ended' is triggered only after 'setmedia' of next track

Note: This is a knowledge-sharing question because I've already found a nice solution.注意:这是一个知识共享问题,因为我已经找到了一个很好的解决方案。

The problem:问题:

The playlist implementation ( http://jplayer.org/latest/demo-02-jPlayerPlaylist/ ) was built by utilising the jPlayer events.播放列表实现( http://jplayer.org/latest/demo-02-jPlayerPlaylist/ )是利用 jPlayer 事件构建的。 One of the side effects of this is that if you want to respond to track start by binding to 'setmedia', and track end by binding to the 'ended' event, you will find that 'ended' is triggered only after the next line was activated.这样做的一个副作用是,如果你想通过绑定到'setmedia'来响应track start,并通过绑定到'ended'事件来响应track end,你会发现'ended'只有在下一行之后才会触发被激活。

This means that the events will fire in this order:这意味着事件将按以下顺序触发:

  • setmedia of track #1轨道#1的setmedia
  • setmedia of track #2 (including moving the jp-playlist-current class)曲目 #2 的setmedia (包括移动jp-playlist-current类)
  • ended of track #1曲目#1结束

So the question is: how to get the 'ended' event be fired before moving to the next track?所以问题是:如何在移动到下一个轨道之前触发“结束”事件?

The root cause of this problem is that the playlist implementation binds itself to the 'ended' event before our binding .这个问题的根本原因是播放列表实现在我们的绑定之前将自己绑定到了“结束”事件

So the solution will be to bind our code before the code of the playlist.所以解决方案是在播放列表的代码之前绑定我们的代码。

In the following answer: jQuery bind event listener before another we can find the following implementation for preBind method:在以下答案中: jQuery 在另一个之前绑定事件侦听器,我们可以找到 preBind 方法的以下实现:

$.fn.preBind = function (type, data, fn) {
    this.each(function () {
        var $this = $(this);

        $this.bind(type, data, fn);

        var currentBindings = $._data(this, 'events')[type];
        if ($.isArray(currentBindings)) {
            currentBindings.unshift(currentBindings.pop());
        }
    });
    return this;
};  

After adding this preBind, we can do the following within the jPlayer 'ready' handler:添加此 preBind 后,我们可以在 jPlayer 'ready' 处理程序中执行以下操作:

$("#jquery_jplayer_1")
    .preBind($.jPlayer.event.ended, function(e) { 
        var cur = $('.jp-playlist').find('li.jp-playlist-current'),
            ix = cur.index(),
            is_last = cur.next().get().length == 0;
        console.log('ended index=' + ix + ', is_last=' + is_last);
    });   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM