【问题标题】:jPlayer not playing songs on playlist object's global declarationjPlayer 不在播放列表对象的全局声明中播放歌曲
【发布时间】:2015-10-08 13:40:07
【问题描述】:

这是我的 js:

$(document).ready(function(){
    var cssSelector = {
        jPlayer: "#jplayer_N",
        cssSelectorAncestor: "#jp_container_N"
    };

    var playlist = [];

    var options = {
        playlistOptions: {
            enableRemoveControls: true,
            autoPlay: true
        },
        swfPath: "js/jPlayer",
        supplied: "webmv, ogv, m4v, oga, mp3",
        smoothPlayBar: true,
        keyEnabled: true,
        audioFullScreen: false
    };

    var myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);

    function loadSong(){
        var song_artist = findArtist();
        var song_title = findTitle();
        var song_poster = findPoster();      

        myPlaylist.add({
            title: song_title,
            artist: song_artist,
            oga: sessionStorage.getItem("file_url"),
            poster: song_poster
        });

        $(document).on($.jPlayer.event.pause, myPlaylist.cssSelector.jPlayer,  function(){
            $('.musicbar').removeClass('animate');
            $('.jp-play-me').removeClass('active');
            $('.jp-play-me').parent('li').removeClass('active');
        });

        $(document).on($.jPlayer.event.play, myPlaylist.cssSelector.jPlayer,  function(){
            $('.musicbar').addClass('animate');
        });

        $(document).on('click', '.jp-play-me', function(e){
            e && e.preventDefault();
            var $this = $(e.target);
            if (!$this.is('a')) $this = $this.closest('a');

            $('.jp-play-me').not($this).removeClass('active');
            $('.jp-play-me').parent('li').not($this.parent('li')).removeClass('active');

            $this.toggleClass('active');
            $this.parent('li').toggleClass('active');
            if( !$this.hasClass('active') ){
                myPlaylist.pause();
            }else{
                var i = Math.floor(Math.random() * (1 + 7 - 1));
                myPlaylist.play(i);
            }
        });
    }    
});

在调用 loadSong() 时,我可以看到我的歌曲在 myPlaylist.playlist 数组中排队。但是,jPlayer 就是不会播放我的歌曲。

此外,不知何故,这似乎对我有用:

$(document).ready(function(){
    var cssSelector = {
        jPlayer: "#jplayer_N",
        cssSelectorAncestor: "#jp_container_N"
    };

    var playlist = [];

    var options = {
        playlistOptions: {
            enableRemoveControls: true,
            autoPlay: true
        },
        swfPath: "js/jPlayer",
        supplied: "webmv, ogv, m4v, oga, mp3",
        smoothPlayBar: true,
        keyEnabled: true,
        audioFullScreen: false
    };

    function loadSong(){
        var song_artist = findArtist();
        var song_title = findTitle();
        var song_poster = findPoster();      

        var myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);

        myPlaylist.add({
            title: song_title,
            artist: song_artist,
            oga: sessionStorage.getItem("file_url"),
            poster: song_poster
        });

        $(document).on($.jPlayer.event.pause, myPlaylist.cssSelector.jPlayer,  function(){
            $('.musicbar').removeClass('animate');
            $('.jp-play-me').removeClass('active');
            $('.jp-play-me').parent('li').removeClass('active');
        });

        $(document).on($.jPlayer.event.play, myPlaylist.cssSelector.jPlayer,  function(){
            $('.musicbar').addClass('animate');
        });

        $(document).on('click', '.jp-play-me', function(e){
            e && e.preventDefault();
            var $this = $(e.target);
            if (!$this.is('a')) $this = $this.closest('a');

            $('.jp-play-me').not($this).removeClass('active');
            $('.jp-play-me').parent('li').not($this.parent('li')).removeClass('active');

            $this.toggleClass('active');
            $this.parent('li').toggleClass('active');
            if( !$this.hasClass('active') ){
                myPlaylist.pause();
            }else{
                var i = Math.floor(Math.random() * (1 + 7 - 1));
                myPlaylist.play(i);
            }
        });
    }    
});

但是,我不能使用它的原因是,每次我调用 loadSong() 函数时,都会创建一个新的 myPlaylist 对象,每次只能播放一首歌曲。当我试图生成一个播放列表时,我想要一个 myPlaylist 对象的静态/全局(不完全确定如何称呼它)实例,使用它我可以将歌曲添加到播放列表中。

根据this 我应该没有任何问题。此外,为了确定 myPlaylist 的范围,我还尝试了 creating static properties and methodsthis,但这也没有任何帮助。我只是无法弄清楚出了什么问题,别管它的具体解决方案。

谁能指出我哪里出错了,在这种情况下可以做什么?

【问题讨论】:

    标签: javascript jquery jplayer


    【解决方案1】:

    解决方案

    您需要将jPlayerPlaylist 初始化和事件处理程序移到loadSong 之外,并使myPlaylist 成为全局变量。

    演示

    有关代码和演示,请参阅this jsFiddle

    【讨论】:

      【解决方案2】:

      抱歉回复晚了。

      感谢@Gyrocode.com 的及时回复,但您的解决方案产生的结果与我的问题中提到的相同。

      对我有用的是:

      $(document).ready(function(){
          var cssSelector = {
              jPlayer: "#jplayer_N",
              cssSelectorAncestor: "#jp_container_N"
          };
      
          var playlist = [];
      
          var options = {
              playlistOptions: {
                  enableRemoveControls: true,
                  autoPlay: true
              },
              swfPath: "js/jPlayer",
              supplied: "webmv, ogv, m4v, oga, mp3",
              smoothPlayBar: true,
              keyEnabled: true,
              audioFullScreen: false
          };
      
          var myPlaylist = null;
      
          function loadSong(){
              var song_artist = findArtist();
              var song_title = findTitle();
              var song_poster = findPoster();      
      
              if(myPlaylist == null)
                  myPlaylist = new jPlayerPlaylist(cssSelector, playlist, options);
      
              myPlaylist.add({
                  title: song_title,
                  artist: song_artist,
                  oga: sessionStorage.getItem("file_url"),
                  poster: song_poster
              });
      
              $(document).on($.jPlayer.event.pause, myPlaylist.cssSelector.jPlayer,  function(){
                  $('.musicbar').removeClass('animate');
                  $('.jp-play-me').removeClass('active');
                  $('.jp-play-me').parent('li').removeClass('active');
              });
      
              $(document).on($.jPlayer.event.play, myPlaylist.cssSelector.jPlayer,  function(){
                  $('.musicbar').addClass('animate');
              });
      
              $(document).on('click', '.jp-play-me', function(e){
                  e && e.preventDefault();
                  var $this = $(e.target);
                  if (!$this.is('a')) $this = $this.closest('a');
      
                  $('.jp-play-me').not($this).removeClass('active');
                  $('.jp-play-me').parent('li').not($this.parent('li')).removeClass('active');
      
                  $this.toggleClass('active');
                  $this.parent('li').toggleClass('active');
                  if( !$this.hasClass('active') ){
                      myPlaylist.pause();
                  }else{
                      var i = Math.floor(Math.random() * (1 + 7 - 1));
                      myPlaylist.play(i);
                  }
              });
          }    
      });
      

      就是这样。如果尚未初始化,只需添加一个 if 来初始化播放列表对象。它就像一个魅力!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 2012-05-15
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多