src/js/pop.js
changeset 843 75ba66457232
parent 841 8da49ff273e0
child 870 2c025db10a10
equal deleted inserted replaced
53:7b55777486c3 843:75ba66457232
       
     1 /* wrapper that simulates popcorn.js because
       
     2    popcorn is a bit unstable at the time */
       
     3 
       
     4 IriSP.PopcornReplacement = {  
       
     5 };
       
     6 
       
     7 /** base class for our popcorn-compatible players.
       
     8  */
       
     9 IriSP.PopcornReplacement.player = function(container, options) {
       
    10   /* the jwplayer calls the callbacks in the global space so we need to 
       
    11      preserve them using IriSP.wrap */
       
    12   this.callbacks = {
       
    13       onReady:  IriSP.wrap(this, this.__initApi),
       
    14       onTime:   IriSP.wrap(this, this.__timeHandler),
       
    15       onPlay:   IriSP.wrap(this, this.__playHandler),
       
    16       onPause:  IriSP.wrap(this, this.__pauseHandler),
       
    17       onSeek:   IriSP.wrap(this, this.__seekHandler) 
       
    18   };
       
    19   
       
    20   this.media = { 
       
    21     "paused": true,
       
    22     "muted": false
       
    23   };
       
    24     
       
    25   this.container = container.slice(1); //eschew the '#'
       
    26   
       
    27   this.msgPump = {}; /* dictionnary used to receive and send messages */
       
    28   this.__codes = []; /* used to schedule the execution of a piece of code in 
       
    29                         a segment (similar to the popcorn.code plugin). */
       
    30   
       
    31   this._options = options;
       
    32                           
       
    33 };
       
    34 
       
    35 IriSP.PopcornReplacement.player.prototype.listen = function(msg, callback) {
       
    36   if (!this.msgPump.hasOwnProperty(msg))
       
    37     this.msgPump[msg] = [];
       
    38 
       
    39   this.msgPump[msg].push(callback);
       
    40 };
       
    41 
       
    42 IriSP.PopcornReplacement.player.prototype.trigger = function(msg, params) {
       
    43   if (!this.msgPump.hasOwnProperty(msg))
       
    44     return;
       
    45 
       
    46   var d = this.msgPump[msg];
       
    47 
       
    48   for(var i = 0; i < d.length; i++) {
       
    49     d[i].call(window, params);
       
    50   }
       
    51 
       
    52 };
       
    53 
       
    54 IriSP.PopcornReplacement.player.prototype.guid = function(prefix) {
       
    55   var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
       
    56       var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
       
    57       return v.toString(16);
       
    58    });
       
    59 
       
    60   return prefix + str;
       
    61 };
       
    62 
       
    63 /** init the api after that flash player has been setup - called by the callback
       
    64     defined by the embedded flash player 
       
    65 */
       
    66 IriSP.PopcornReplacement.player.prototype.__initApi = function() {
       
    67   this.trigger("loadedmetadata"); // we've done more than loading metadata of course,
       
    68                                                       // but popcorn doesn't need to know more.
       
    69   this.media.muted = this.playerFns.getMute();
       
    70   /* some programmed segments are supposed to be run at the beginning */
       
    71   var i = 0;
       
    72   for(i = 0; i < this.__codes.length; i++) {
       
    73     var c = this.__codes[i];
       
    74     if (0 == c.start) {
       
    75       c.onStart();
       
    76     }
       
    77     
       
    78     if (0 == c.end) {
       
    79       c.onEnd();
       
    80     }
       
    81   }
       
    82 };
       
    83 
       
    84 /*
       
    85 IriSP.PopcornReplacement.jwplayer = function(container, options) {
       
    86   IriSP.PopcornReplacement._container = container.slice(1); //eschew the '#'
       
    87   options.events = {
       
    88       onReady: IriSP.PopcornReplacement.__initApi,
       
    89       onTime: IriSP.PopcornReplacement.__timeHandler,
       
    90       onPlay: IriSP.PopcornReplacement.__playHandler,
       
    91       onPause: IriSP.PopcornReplacement.__pauseHandler,
       
    92       onSeek: IriSP.PopcornReplacement.__seekHandler 
       
    93       }
       
    94     
       
    95   jwplayer(IriSP.PopcornReplacement._container).setup(options);
       
    96   IriSP.PopcornReplacement.media.duration = options.duration;
       
    97   return IriSP.PopcornReplacement;
       
    98 };
       
    99 */
       
   100 
       
   101 IriSP.PopcornReplacement.player.prototype.currentTime = function(time) {
       
   102   if (typeof(time) === "undefined") {        
       
   103       return this.playerFns.getPosition();            
       
   104   } else {
       
   105      var currentTime = +time;
       
   106      this.playerFns.seek(currentTime);              
       
   107      return currentTime;
       
   108   }
       
   109 };
       
   110 
       
   111 IriSP.PopcornReplacement.player.prototype.play = function() {
       
   112   this.media.paused = false;
       
   113   this.trigger("play");
       
   114   //IriSP.PopcornReplacement.trigger("playing");
       
   115   this.playerFns.play();
       
   116 };
       
   117     
       
   118 IriSP.PopcornReplacement.player.prototype.pause = function() {
       
   119 //  if ( !this.media.paused ) {
       
   120     this.media.paused = true;
       
   121     this.trigger( "pause" );
       
   122     this.playerFns.pause();
       
   123 //  }
       
   124 };
       
   125 
       
   126 IriSP.PopcornReplacement.player.prototype.muted = function(val) {
       
   127   if (typeof(val) !== "undefined") {
       
   128 
       
   129     if (this.playerFns.getMute() !== val) {
       
   130       if (val) {
       
   131         this.playerFns.setMute(true);
       
   132         this.media.muted = true;
       
   133       } else {
       
   134         this.playerFns.setMute(false);
       
   135         this.media.muted = false;
       
   136       }
       
   137 
       
   138       this.trigger( "volumechange" );
       
   139     }
       
   140     
       
   141     return this.playerFns.getMute();
       
   142   } else {
       
   143     return this.playerFns.getMute();
       
   144   }
       
   145 };
       
   146 
       
   147 IriSP.PopcornReplacement.player.prototype.volume = function(val) {
       
   148     if (typeof this.playerFns.getVolume == "undefined" || typeof this.playerFns.setVolume == "undefined") {
       
   149         return false;
       
   150     }
       
   151     var _vol = this.playerFns.getVolume();
       
   152     if (typeof(val) !== "undefined" && parseFloat(val) !== NaN) {
       
   153         val = Math.max(0, Math.min(1, val));
       
   154         if (parseFloat(val) != parseFloat(_vol)) {
       
   155             this.playerFns.setVolume(val);
       
   156             this.trigger("volumechange");
       
   157             _vol = this.playerFns.getVolume();
       
   158         }
       
   159     }
       
   160     return _vol;
       
   161 };
       
   162 
       
   163 IriSP.PopcornReplacement.player.prototype.mute = IriSP.PopcornReplacement.player.prototype.muted;
       
   164 
       
   165 IriSP.PopcornReplacement.player.prototype.code = function(options) {
       
   166   this.__codes.push(options);
       
   167   return this;
       
   168 };
       
   169 
       
   170 /* called everytime the player updates itself 
       
   171    (onTime event)
       
   172  */
       
   173 
       
   174 IriSP.PopcornReplacement.player.prototype.__timeHandler = function(event) {
       
   175   var pos = event.position;
       
   176 
       
   177   var i = 0;
       
   178   for(i = 0; i < this.__codes.length; i++) {
       
   179      var c = this.__codes[i];
       
   180 
       
   181      if (pos >= c.start && pos < c.end && 
       
   182          pos - 1 <= c.start) {       
       
   183         c.onStart();
       
   184      }
       
   185  
       
   186      if (pos > c.start && pos > c.end && 
       
   187          pos - 1 <= c.end) {
       
   188          c.onEnd();
       
   189      }
       
   190    
       
   191   }
       
   192  
       
   193   this.trigger("timeupdate");
       
   194 };
       
   195 
       
   196 IriSP.PopcornReplacement.player.prototype.__seekHandler = function(event) {
       
   197   var i = 0;
       
   198   
       
   199   for(i = 0; i < this.__codes.length; i++) {
       
   200      var c = this.__codes[i];
       
   201     
       
   202      if (event.position >= c.start && event.position < c.end) {        
       
   203         c.onEnd();
       
   204      }         
       
   205    }
       
   206   
       
   207    for(i = 0; i < this.__codes.length; i++) {
       
   208      var c = this.__codes[i];
       
   209 
       
   210      if (typeof(event.offset) === "undefined")
       
   211        event.offset = 0;
       
   212            
       
   213      if (event.offset >= c.start && event.offset < c.end) { 
       
   214        c.onStart();
       
   215      }
       
   216      
       
   217    }
       
   218   
       
   219   /* this signal sends as an extra argument the position in the video.
       
   220      As far as I know, this argument is not provided by popcorn */
       
   221   this.trigger("seeked", event.offset);  
       
   222 };
       
   223 
       
   224 IriSP.PopcornReplacement.player.prototype.__playHandler = function(event) {
       
   225   this.media.paused = false;
       
   226   this.trigger("play");
       
   227 };
       
   228 
       
   229 IriSP.PopcornReplacement.player.prototype.__pauseHandler = function(event) {
       
   230   this.media.paused = true;
       
   231   this.trigger("pause");
       
   232 };
       
   233 
       
   234 IriSP.PopcornReplacement.player.prototype.roundTime = function() {
       
   235   var currentTime = this.currentTime();
       
   236   return Math.round(currentTime);
       
   237 };