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

jplayer.org save playlist in cookies

我有一个可用的 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 */
});

此处触发cookie存储,使用标准的jplayer函数:

$("#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(current) 不会触发 bind($.jPlayer.event.play) 并且我强制存储 cookie 的最后两行不起作用。我还尝试删除该行并放置 "true",以测试它是否触发 bind.event.play 但它不起作用。

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

这是 cookie 存储限制。我只是使用本地存储绕过了限制,例如:

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

而不是

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