src/js/pop.js
branchpopcorn-port
changeset 434 0907b73a1f00
parent 431 f9460dc1957f
child 441 99b7c5192330
equal deleted inserted replaced
424:dcf7121b1202 434:0907b73a1f00
     1 /* wrapper that simulates popcorn.js because
     1 /* wrapper that simulates popcorn.js because
     2    popcorn is a bit unstable at the time */
     2    popcorn is a bit unstable at the time */
     3 
     3 
     4 PopcornReplacement = {};
     4 IriSP.PopcornReplacement = {};
     5 PopcornReplacement.media = { "paused": true};
     5 IriSP.PopcornReplacement.media = { "paused": true};
     6 
     6 
     7 PopcornReplacement.listen = function(msg, callback) {
     7 IriSP.PopcornReplacement.listen = function(msg, callback) {
     8   IriSP.jQuery(PopcornReplacement).bind(msg, function(event, rest) { callback(rest); });
     8   IriSP.jQuery(IriSP.PopcornReplacement).bind(msg, function(event, rest) { callback(rest); });
     9 };
     9 };
    10 
    10 
    11 PopcornReplacement.trigger = function(msg, params) {
    11 IriSP.PopcornReplacement.trigger = function(msg, params) {
    12   IriSP.jQuery(PopcornReplacement).trigger(msg, params);
    12   IriSP.jQuery(IriSP.PopcornReplacement).trigger(msg, params);
    13 };
    13 };
    14 
    14 
    15 PopcornReplacement.guid = function(prefix) {
    15 IriSP.PopcornReplacement.guid = function(prefix) {
    16   var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    16   var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    17       var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    17       var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
    18       return v.toString(16);
    18       return v.toString(16);
    19    });
    19    });
    20 
    20 
    21   return prefix + str;
    21   return prefix + str;
    22 };
    22 };
    23 
    23 
    24 PopcornReplacement.__initApi = function() {
    24 IriSP.PopcornReplacement.__initApi = function() {
    25   PopcornReplacement.trigger("timeupdate");
    25   IriSP.PopcornReplacement.trigger("timeupdate");
    26 };
    26 };
    27 
    27 
    28 PopcornReplacement.jwplayer = function(container, options) {
    28 IriSP.PopcornReplacement.jwplayer = function(container, options) {
    29   PopcornReplacement._container = container.slice(1); //eschew the '#'
    29   IriSP.PopcornReplacement._container = container.slice(1); //eschew the '#'
    30   options.events = {
    30   options.events = {
    31       onReady: PopcornReplacement.__initApi,
    31       onReady: IriSP.PopcornReplacement.__initApi,
    32       onTime: PopcornReplacement.__timeHandler,
    32       onTime: IriSP.PopcornReplacement.__timeHandler,
    33       onSeek: PopcornReplacement.__seekHandler }
    33       onSeek: IriSP.PopcornReplacement.__seekHandler }
    34     
    34     
    35   jwplayer(PopcornReplacement._container).setup(options);
    35   jwplayer(IriSP.PopcornReplacement._container).setup(options);
    36   PopcornReplacement.media.duration = options.duration;
    36   IriSP.PopcornReplacement.media.duration = options.duration;
    37   return PopcornReplacement;
    37   return IriSP.PopcornReplacement;
    38 };
    38 };
    39 
    39 
    40 PopcornReplacement.currentTime = function(time) {
    40 IriSP.PopcornReplacement.currentTime = function(time) {
    41   if (typeof(time) === "undefined") {
    41   if (typeof(time) === "undefined") {
    42       return jwplayer(PopcornReplacement._container).getPosition();            
    42       return jwplayer(IriSP.PopcornReplacement._container).getPosition();            
    43   } else {
    43   } else {
    44      var currentTime = +time;
    44      var currentTime = +time;
    45      jwplayer( PopcornReplacement._container ).seek( currentTime );
    45      jwplayer( IriSP.PopcornReplacement._container ).seek( currentTime );
    46      return jwplayer(PopcornReplacement._container).getPosition();            
    46      return jwplayer(IriSP.PopcornReplacement._container).getPosition();            
    47   }
    47   }
    48 };
    48 };
    49 
    49 
    50 PopcornReplacement.play = function() {
    50 IriSP.PopcornReplacement.play = function() {
    51       PopcornReplacement.media.paused = false;
    51       IriSP.PopcornReplacement.media.paused = false;
    52 //      PopcornReplacement.trigger("play");
    52 //      IriSP.PopcornReplacement.trigger("play");
    53 //      PopcornReplacement.trigger("playing");
    53 //      IriSP.PopcornReplacement.trigger("playing");
    54       jwplayer( PopcornReplacement._container ).play();
    54       jwplayer( IriSP.PopcornReplacement._container ).play();
    55 };
    55 };
    56     
    56     
    57 PopcornReplacement.pause = function() {
    57 IriSP.PopcornReplacement.pause = function() {
    58       if ( !PopcornReplacement.media.paused ) {
    58       if ( !IriSP.PopcornReplacement.media.paused ) {
    59         PopcornReplacement.media.paused = true;
    59         IriSP.PopcornReplacement.media.paused = true;
    60         PopcornReplacement.trigger( "pause" );
    60         IriSP.PopcornReplacement.trigger( "pause" );
    61         jwplayer( PopcornReplacement._container ).pause();
    61         jwplayer( IriSP.PopcornReplacement._container ).pause();
    62       }
    62       }
    63 };
    63 };
    64 
    64 
    65 PopcornReplacement.muted = function(val) {
    65 IriSP.PopcornReplacement.muted = function(val) {
    66   if (typeof(val) !== "undefined") {
    66   if (typeof(val) !== "undefined") {
    67 
    67 
    68     if (jwplayer(PopcornReplacement._container).getMute() !== val) {
    68     if (jwplayer(IriSP.PopcornReplacement._container).getMute() !== val) {
    69       if (val) {
    69       if (val) {
    70         jwplayer(PopcornReplacement._container).setMute(true);
    70         jwplayer(IriSP.PopcornReplacement._container).setMute(true);
    71       } else {
    71       } else {
    72         jwplayer( PopcornReplacement._container ).setMute(false);
    72         jwplayer( IriSP.PopcornReplacement._container ).setMute(false);
    73       }
    73       }
    74 
    74 
    75       PopcornReplacement.trigger( "volumechange" );
    75       IriSP.PopcornReplacement.trigger( "volumechange" );
    76     }
    76     }
    77     
    77     
    78     return jwplayer( PopcornReplacement._container ).getMute();
    78     return jwplayer( IriSP.PopcornReplacement._container ).getMute();
    79   } else {
    79   } else {
    80     return jwplayer( PopcornReplacement._container ).getMute();
    80     return jwplayer( IriSP.PopcornReplacement._container ).getMute();
    81   }
    81   }
    82 };
    82 };
    83 
    83 
    84 PopcornReplacement.mute = PopcornReplacement.muted;
    84 IriSP.PopcornReplacement.mute = IriSP.PopcornReplacement.muted;
    85 
    85 
    86 PopcornReplacement.__codes = [];
    86 IriSP.PopcornReplacement.__codes = [];
    87 PopcornReplacement.code = function(options) {
    87 IriSP.PopcornReplacement.code = function(options) {
    88   PopcornReplacement.__codes.push(options);
    88   IriSP.PopcornReplacement.__codes.push(options);
    89   return PopcornReplacement;
    89   return IriSP.PopcornReplacement;
    90 };
    90 };
    91 
    91 
    92 PopcornReplacement.__runCode = function() {
    92 IriSP.PopcornReplacement.__runCode = function() {
    93   var currentTime = jwplayer(PopcornReplacement._container).getPosition();
    93   var currentTime = jwplayer(IriSP.PopcornReplacement._container).getPosition();
    94   var i = 0;
    94   var i = 0;
    95   for(i = 0; i < PopcornReplacement.__codes.length; i++) {
    95   for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) {
    96     var c = PopcornReplacement.__codes[i];
    96     var c = IriSP.PopcornReplacement.__codes[i];
    97     if (currentTime == c.start) {
    97     if (currentTime == c.start) {
    98       c.onStart();
    98       c.onStart();
    99     }
    99     }
   100     
   100     
   101     if (currentTime == c.end) {
   101     if (currentTime == c.end) {
   107 
   107 
   108 /* called everytime the player updates itself 
   108 /* called everytime the player updates itself 
   109    (onTime event)
   109    (onTime event)
   110  */
   110  */
   111 
   111 
   112 PopcornReplacement.__timeHandler = function(event) {
   112 IriSP.PopcornReplacement.__timeHandler = function(event) {
   113   var pos = event.position;
   113   var pos = event.position;
   114 
   114 
   115   var i = 0;
   115   var i = 0;
   116   for(i = 0; i < PopcornReplacement.__codes.length; i++) {
   116   for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) {
   117      var c = PopcornReplacement.__codes[i];
   117      var c = IriSP.PopcornReplacement.__codes[i];
   118      
   118      
   119      if (pos >= c.start && pos < c.end && 
   119      if (pos >= c.start && pos < c.end && 
   120          pos - 0.1 <= c.start) {
   120          pos - 0.1 <= c.start) {
   121         c.onStart();
   121         c.onStart();
   122      }
   122      }
   126         c.onEnd();
   126         c.onEnd();
   127      }
   127      }
   128    
   128    
   129   }
   129   }
   130  
   130  
   131   PopcornReplacement.trigger("timeupdate");
   131   IriSP.PopcornReplacement.trigger("timeupdate");
   132 };
   132 };
   133 
   133 
   134 PopcornReplacement.__seekHandler = function(event) { 
   134 IriSP.PopcornReplacement.__seekHandler = function(event) { 
   135   var i = 0;
   135   var i = 0;
   136   for(i = 0; i < PopcornReplacement.__codes.length; i++) {
   136   for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) {
   137      var c = PopcornReplacement.__codes[i];
   137      var c = IriSP.PopcornReplacement.__codes[i];
   138     
   138     
   139      if (event.position >= c.start && event.position < c.end) {
   139      if (event.position >= c.start && event.position < c.end) {
   140         c.onEnd();
   140         c.onEnd();
   141      }
   141      }
   142     
   142     
   146        c.onStart();
   146        c.onStart();
   147      }
   147      }
   148      
   148      
   149    }
   149    }
   150 
   150 
   151   PopcornReplacement.trigger("timeupdate");
   151   IriSP.PopcornReplacement.trigger("timeupdate");
   152 }
   152 }
   153 
   153 
   154 
   154 
   155 PopcornReplacement.roundTime = function() {
   155 IriSP.PopcornReplacement.roundTime = function() {
   156   var currentTime = PopcornReplacement.currentTime();
   156   var currentTime = IriSP.PopcornReplacement.currentTime();
   157   return Math.round(currentTime);
   157   return Math.round(currentTime);
   158 };
   158 };