jplayer.org将播放列表保存在cookie中

时间:2017-03-22 18:23:48

标签: jquery ajax jplayer

我有一个正在运作的Jplayer,我很自豪能在这里分享它,为每个人带来便利。

当我在其中添加单个曲目时,它会记住cookie中的播放列表。这是工作代码:

// PLAY SINGLE SONG
// el delegation is needed because songs are dynamically loaded via ajax datatables
$(document).on("click", '.playable', function(el) {
    var title = $(this).data("title");
    var artist = $(this).data("artist");
    var mp3 = $(this).data("mp3");
    var poster = $(this).data("poster");
    myPlaylist.add({
        title: title,
        artist: artist,
        mp3: mp3,
        poster: poster
    }, true ); /* true makes it play on load */
});

此处使用标准的jplayer功能触发cookie存储:

$("#jquery_jplayer").bind($.jPlayer.event.play, function(el) {
    Cookies.remove('jp_playlist');
    Cookies.set('jp_playlist', JSON.stringify(myPlaylist) );
});

但我还有一个ADD ALL按钮,它会映射每首歌并将其添加到播放列表中:

// PLAY ALL
$(document).on("click", '#play_all', function(el) {

    // play all
    $.map($('.playable'), function(el) {
        var title = $(el).data("title");
        var artist = $(el).data("artist");
        var mp3 = $(el).data("mp3");
        var poster = $(el).data("poster");
        myPlaylist.add({
            title: title,
            artist: artist,
            mp3: mp3,
            poster: poster
        });
    });

    var current = myPlaylist.current;
    myPlaylist.play(current); /* if I would have put "true" like above it would have started the last song instead of the first */

    Cookies.remove('jp_playlist');
    Cookies.set('jp_playlist', JSON.stringify(myPlaylist) );
});

未设置Cookie,并且在重新加载页面时不会保留播放列表。 我提醒(JSON.stringify(myPlaylist)),一切都很好。似乎myPlaylist.play(当前)没有触发绑定($。jPlayer.event.play)和我的最后2行强制cookie存储不起作用。我还尝试删除那些行并将" true",以测试它是否触发bind.event.play但它不起作用。

提前感谢您的时间和任何提示。

1 个答案:

答案 0 :(得分:0)

这是一个cookie存储限制。我只是使用localstorage绕过了限制,例如:

localStorage.setItem('jp_playlist', JSON.stringify(myPlaylist) );
localStorage.removeItem('jp_playlist');

而不是

Cookies.set('jp_playlist', JSON.stringify(myPlaylist) );
Cookies.remove('jp_playlist');