2010-09-03 70 views
0

好的,所以我有这个非常酷的Mario主题媒体播放器,由极其强大的jQuery插件jplayer的定制创建。jquery jplayer cookie首选项

所以,我想给用户选择他或她是否想在网站的后台自动启动音乐播放。

我有它默认关闭的,因为这应该是一个企业,甚至是定期的,非盈利的网站,在背景音乐可以非常刺激性,尤其是如果它不清楚如何启动和停止音频。

无论如何,我试图设置一个cookie来做到这一点,使用直观的jQuery cookie plugin

下面是代码我设置被点击的模式对话框的按钮时:

buttons: { 
    'Without Music': function() { 
    $(this).dialog('close'); 
    $.cookie('autoPlay', 'no', { expires: 365 * 10 }); 
    }, 
    'With Music': function() { 
    $(this).dialog('close'); 
    $.cookie('autoPlay', 'yes', { expires: 365 * 10 }); 
    } 
} 

现在,我正在一个蒂莫,这是检查每毫秒(是的,这很容易被调整,但我只是想即时结果),用于将autoPlay cookie的值,无论是yes或no!

setInterval(function() { 
    if ($.cookie('autoPlay') == no) { 
    displayPlayList(); 
    playListInit(false); // Parameter is a boolean for autoplay. 
    } 
}, 1); // checks every millisecond (i.e. 1/1000 of a second) 
    // need to do speed tests, and see if fast checking results in bogged down pages, difference between 1 millisecond and even 100-300 are nearly negligible 
setInterval(function() { 
    if ($.cookie('autoPlay') == yes) { 
    displayPlayList(); 
    playListInit(false); // Parameter is a boolean for autoplay. 
    } 

所以,出于某种原因,当我刷新页面,之后选择没错。播放器不会自动播放。现在,我认为这个问题可能是我的条件陈述的原因。但我不知道..

UPDATE:

这里是我的整个新jplayer.js文件,其中包括我们需要对这个问题的工作代码:

$(document).ready(function(){ 

    $("#jpId").jPlayer({ 
     swfPath: "/js" 
    }); 

    var playItem = 0; 

    var myPlayList = [ 
     {name:"SMB Overworld",mp3:"/audio/MushroomKingdomSMB.mp3"}, 
     {name:"SMB Underworld",mp3:"/audio/UnderworldSMB.mp3"}, 
     {name:"SMB Underwater",mp3:"/audio/UnderwaterSMB.mp3"}, 
     {name:"SMW Castle",mp3:"/audio/CastleSMW.mp3"} 
    ]; 

    // Local copy of jQuery selectors, for performance. 
    var jpPlayTime = $("#jplayer_play_time"); 
    var jpTotalTime = $("#jplayer_total_time"); 

    $("#jquery_jplayer").jPlayer({ 
     ready: function() { 
      displayPlayList(); 
      playListInit(false); // Parameter is a boolean for autoplay. 
     } 
    }) 
    .jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) { 
     jpPlayTime.text($.jPlayer.convertTime(playedTime)); 
     jpTotalTime.text($.jPlayer.convertTime(totalTime)); 
    }) 
    .jPlayer("onSoundComplete", function() { 
     playListNext(); 
    }); 

    $("#jplayer_previous").click(function() { 
     playListPrev(); 
     $(this).blur(); 
     return false; 
    }); 

    $("#jplayer_next").click(function() { 
     playListNext(); 
     $(this).blur(); 
     return false; 
    }); 

    function displayPlayList() { 
     $("#jplayer_playlist ul").empty(); 
     for (i=0; i < myPlayList.length; i++) { 
      var listItem = (i == myPlayList.length-1) ? "<li class='jplayer_playlist_item_last'>" : "<li>"; 
      listItem += "<a href='#' id='jplayer_playlist_item_"+i+"' tabindex='1'>"+ myPlayList[i].name +"</a></li>"; 
      $("#jplayer_playlist ul").append(listItem); 
      $("#jplayer_playlist_item_"+i).data("index", i).click(function() { 
       var index = $(this).data("index"); 
       if (playItem != index) { 
        playListChange(index); 
       } else { 
        $("#jquery_jplayer").jPlayer("play"); 
       } 
       $(this).blur(); 
       return false; 
      }); 
     } 
    } 

    function playListInit(autoplay) { 
     if(autoplay) { 
      playListChange(playItem); 
     } else { 
      playListConfig(playItem); 
     } 
    } 

    function playListConfig(index) { 
     $("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current").parent().removeClass("jplayer_playlist_current"); 
     $("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current").parent().addClass("jplayer_playlist_current"); 
     playItem = index; 
     $("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].ogg); 
    } 

    function playListChange(index) { 
     playListConfig(index); 
     $("#jquery_jplayer").jPlayer("play"); 
    } 

    function playListNext() { 
     var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0; 
     playListChange(index); 
    } 

    function playListPrev() { 
     var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1; 
     playListChange(index); 
    } 
    $('#text_music').click(function() { 
     $('#jplayer').slideToggle(500); 
    }); 

    $("#player").bind("clickoutside", function(event){ 
     if($('#jplayer').is(':visible')) { 
      $('#jplayer').slideToggle(500); 
     } 
    }); 

    setInterval(function() { 

     if($('a#jplayer_playlist_item_0').hasClass('jplayer_playlist_current')) { 
     $("#bg_3, #bg_4, #map_4, #sprites_4, #platforms_4, #bg_5, #bg_6, #map_6, #sprites_6, #bg_7, #bg_8, #map_8, #sprites_8, #behindsprites_8").hide(); 
     $("#bg_1, #bg_2, #map_2, #sprites_2").show(); 
     }; 

     if($('a#jplayer_playlist_item_1').hasClass('jplayer_playlist_current')) { 
     $("#bg_1, #bg_2, #map_2, #sprites_2, #bg_5, #bg_6, #map_6, #sprites_6, #bg_7, #bg_8, #map_8, #sprites_8, #behindsprites_8").hide(); 
     $("#bg_3, #bg_4, #map_4, #sprites_4, #platforms_4").show(); 
     }; 

     if($('a#jplayer_playlist_item_2').hasClass('jplayer_playlist_current')) { 
     $("#bg_1, #bg_2, #map_2, #sprites_2, #bg_3, #bg_4, #map_4, #sprites_4, #platforms_4, #bg_7, #bg_8, #map_8, #sprites_8, #behindsprites_8").hide(); 
     $("#bg_5, #bg_6, #map_6, #sprites_6").show(); 
     }; 

     if($('a#jplayer_playlist_item_3').hasClass('jplayer_playlist_current')) { 
     $("#bg_1, #bg_2, #map_2, #sprites_2, #bg_3, #bg_4, #map_4, #sprites_4, #platforms_4, #bg_5, #bg_6, #map_6, #sprites_6").hide(); 
     $("#bg_7, #bg_8, #map_8, #sprites_8, #behindsprites_8").show(); 
     }; 
    }, 1); // checks every millisecond (i.e. 1/1000 of a second) 
      // need to do speed tests, and see if fast checking results in bogged down pages, difference between 1 millisecond and even 100-300 are nearly negligible 

    setInterval(function() { 
     if ($.cookie('autoPlay') === 'no') { 
      displayPlayList(); 
      playListInit(false); // Parameter is a boolean for autoplay. 
     } 
    }, 1); // checks every millisecond (i.e. 1/1000 of a second) 
     // need to do speed tests, and see if fast checking results in bogged down pages, difference between 1 millisecond and even 100-300 are nearly negligible 
    setInterval(function() { 
     if ($.cookie('autoPlay') === 'yes') { 
      displayPlayList(); 
      playListInit(true); // Parameter is a boolean for autoplay. 
     } 
    }, 1); // checks every millisecond (i.e. 1/1000 of a second) 
     // need to do speed tests, and see if fast checking results in bogged down pages, difference between 1 millisecond and even 100-300 are nearly negligible  
    /* 
    $('#jquery_jplayer') 
    playListInit(true) 
    */ 
    $('#infobutton').click(function() { 
     $('#music_descrip').dialog('open'); 
} ); 
    $('#music_descrip').dialog({ 
     title: '<img src="/images/text/text_mario_planet_jukebox.png" id="text_mario_planet_jukebox"/>', 
     autoOpen: false, 
     height: 375, 
     width: 500, 
     modal: true, 
     resizable: false, 
     buttons: { 
      'Without Music': function() { 
       $(this).dialog('close'); 
       $.cookie('autoPlay', 'no', { expires: 365 * 10 }); 
      }, 
      'With Music': function() { 
       $(this).dialog('close'); 
       $.cookie('autoPlay', 'yes', { expires: 365 * 10 }); 
      } 
     } 
    }); 

}); 
--> 

它似乎仍然有同样的问题..我试图解决它,但如果您在我之前发现错误,请让我知道! :)

回答

1

,则需要比较到一个字符串,像这样:

if ($.cookie('autoPlay') == 'yes') { 

即使你检查yes,你仍然传递false另外,现在看来似乎应该是true

了间隔50毫秒,至少,这样的整体:

setInterval(function() { 
    displayPlayList(); 
    playListInit($.cookie('autoPlay') === 'yes'); 
}, 1); 

虽然在一般,也不是检查在间隔一个cookie在所有,作出力所能及的执行效能的动作既设置cookie,然后执行操作...仅当使用 cookie时,首次在document.ready中设置播放器。

+0

感谢您的帮助,但目前我仍然遇到同样的问题。 查看我在更新中发布的新代码。我只是尝试cookie的方式,直到我可以得到它的工作,只是因为它应该工作imo .. – Qcom 2010-09-03 14:51:34