简体   繁体   English

jPlayer无法在播放列表对象的全局声明中播放歌曲

[英]jPlayer not playing songs on playlist object's global declaration

Here is my js: 这是我的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);
            }
        });
    }    
});

On calling loadSong(), I can see that my songs are getting queued in the myPlaylist.playlist array. 调用loadSong()时,可以看到我的歌曲在myPlaylist.playlist数组中排队。 However, jPlayer just won't play my songs. 但是,jPlayer不会播放我的歌曲。

Also, somehow, this however seems to work for me: 另外,无论如何,这似乎对我有用:

$(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);
            }
        });
    }    
});

But, the reason I can't use this is, every time I call the loadSong() function, a new myPlaylist object gets created every single time allowing me to play only one song at a time. 但是,我不能使用它的原因是,每次调用loadSong()函数时,都会每次创建一个新的myPlaylist对象,使我一次只能播放一首歌曲。 As I'm trying to generate a playlist, I'd want to have a static/global (not exactly sure what to call it) instance of myPlaylist object using which I would add songs to the playlist. 在尝试生成播放列表时,我希望有一个myPlaylist对象的静态/全局(不确定要调用什么)实例,通过该实例,我可以将歌曲添加到播放列表中。

According to this I should not have any problem. 根据这个我不应该有任何问题。 Also, just to be sure about the scope of myPlaylist, I also tried my hand on creating static properties and methods and this , but that did not turn out to be of any help as well. 另外,为了确保myPlaylist的范围,我还尝试了创建静态属性和方法以及this ,但是这也没有任何帮助。 I'm just not able to figure out what's wrong, leave alone working for a concrete solution. 我只是无法弄清楚出什么问题了,更不用说具体的解决方案了。

Can anyone please point out as to where I'm going wrong and what can be done in that case? 任何人都可以指出我要去哪里,在这种情况下可以做什么?

SOLUTION

You need to move jPlayerPlaylist initialization and event handlers outside of loadSong and make myPlaylist a global variable. 你需要移动jPlayerPlaylist初始化和事件处理程序之外loadSong ,使myPlaylist一个全局变量。

DEMO DEMO

See this jsFiddle for code and demonstration. 有关代码和演示,请参见此jsFiddle

Apologies for the late reply. 抱歉回复晚。

Thank you @Gyrocode.com for your prompt reply but your solution generated the same results as mentioned in my question. 感谢@ Gyrocode.com的迅速答复,但您的解决方案产生了与我的问题中提到的结果相同的结果。

What worked for me was: 对我有用的是:

$(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);
            }
        });
    }    
});

That's it. 而已。 Simply added an if to initialize the playlist object if not already initialized. 只需添加一个if即可初始化播放列表对象(如果尚未初始化)。 And it worked like a charm! 它就像一个魅力!

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

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