开发者

Javascript pattern with jPlayer

开发者 https://www.devze.com 2023-03-12 03:03 出处:网络
I just build a javascript pattern to use jPlayer in web browser. Like this code: var myplayer = {} myplayer.Controller = (function() {

I just build a javascript pattern to use jPlayer in web browser. Like this code:

var myplayer = {}
myplayer.Controller = (function() {
        var controller;
        var player_state= "default";
        var player  = jQuery('#jquery_jplayer_2');

    function createController(){
        function initial(){
            player.jPlayer({
                solution: "flash, html",
                supplied: "mp3",
                swfPath: "/js"
            });
           }
           function play(){
              player.jPlayer("play");
           }
            return {
              initial:function(){
                  initial();
              },
              play:function(){
                  play();
              }
            }
     }

     return {
         getController:function(){
             if(!controller){
                 controller = createController();
             }
             return controller;
         }
     }

and I call it's by var mypl = myplayer.controller.开发者_如何学GogetController().initial(); It's run without any error or warning but not initial jPlayer? Anyone here have an experience on javascript please show me the way, thank you very much! I also check path to javascript folder and mp3 file is valid. I write alert before call and after call initial function and it run, but still not initial jPlayer. help me please, please!!!


You're being too tricky. Look at the source from this demo

http://jplayer.org/latest/demo-01/

and use something very similar to that.


This code below worked for me so I post here if someone interesting. I'm appreciate any comment from anyone.

var mypl = {};
mypl.player = (function(){
    var myplayer;
    var pl = jQuery("#jquery_jplayer_2").jPlayer;

    function createPlayer(){
        function initialPlayer(domitem){
            if(domitem == undefined){
                domitem = $("#jquery_jplayer_2"); 
            }
            domitem.jPlayer( {
                ready: function () {
                  $(this).jPlayer("setMedia", {
                    mp3: "url_to_mp3_file"
                  });
                },
                solution: "flash, html",
                supplied: "mp3",
                swfPath: "path_to_swf_file_locate"
              });
        }
        function setMedia(title,filepath){
            jQuery("#jquery_jplayer_2").jPlayer("setMedia",{name:title,free:true,mp3:filepath});
        }
        function play(){
            jQuery("#jquery_jplayer_2").jPlayer("play");
        }
        function pause(){
            jQuery("#jquery_jplayer_2").jPlayer("pause");
        }
        function stop(){
            jQuery("#jquery_jplayer_2").jPlayer("stop");
        }
        function volume(volume){
            if(volume == undefined || volume  < 0){
                volume = 0.8;
            }
            jQuery("#jquery_jplayer_2").jPlayer("volume",volume);
        }
        function mute(){
            jQuery("#jquery_jplayer_2").jPlayer("mute");
        }
        function unmute(){
            jQuery("#jquery_jplayer_2").jPlayer("unmute");
        }
        function seek(seek){
            if(seek == undefined || seek < 0){
                seek = 0;
            }
            jQuery("#jquery_jplayer_2").jPlayer("playHead",seek);
        }
        return{
            initialplayer:function(domitem){
                initialPlayer(domitem);
            },
            setmedia:function(title,filepath){
                setMedia(title,filepath);
            },
            play:function(){
                play();
            },
            pause:function(){
                pause();
            },
            stop:function(){
                stop();
            },
            mute:function(){
                mute();
            },
            unmute:function(){
                unmute();
            },
            seek:function(){
                seek();
            }
        }
    }
    return{
        getPlayer:function(){
            if(!myplayer){
                myplayer = createPlayer();
            }
            else{
            }
            return myplayer;
        }
    }
})();

and I call it like this:

var abc = mypl.player.getPlayer();
abc.initialplayer();

when you click on some element just call abc.play() or abc.stop(). I still can not make abc.pause() work at this time. Hope to fixed this soon!! I've changed function name from pause to mypause and function volume too, it run but I still don't know why it run or not!!

0

精彩评论

暂无评论...
验证码 换一张
取 消