src/js/pop.js
branchpopcorn-port
changeset 419 fe7e46eda1b4
parent 417 bae7c50704d7
child 431 f9460dc1957f
equal deleted inserted replaced
418:06d2aa32c5f4 419:fe7e46eda1b4
     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 Popcorn = {};
     4 PopcornReplacement = {};
     5 Popcorn.media = { "paused": true};
     5 PopcornReplacement.media = { "paused": true};
     6 
     6 
     7 Popcorn.listen = function(msg, callback) {
     7 PopcornReplacement.listen = function(msg, callback) {
     8   IriSP.jQuery(Popcorn).bind(msg, function(event, rest) { callback(rest); });
     8   IriSP.jQuery(PopcornReplacement).bind(msg, function(event, rest) { callback(rest); });
     9 };
     9 };
    10 
    10 
    11 Popcorn.trigger = function(msg, params) {
    11 PopcornReplacement.trigger = function(msg, params) {
    12   IriSP.jQuery(Popcorn).trigger(msg, params);
    12   IriSP.jQuery(PopcornReplacement).trigger(msg, params);
    13 };
    13 };
    14 
    14 
    15 Popcorn.guid = function(prefix) {
    15 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 Popcorn.__initApi = function() {
    24 PopcornReplacement.__initApi = function() {
    25   Popcorn.trigger("timeupdate");
    25   PopcornReplacement.trigger("timeupdate");
    26 };
    26 };
    27 
    27 
    28 Popcorn.jwplayer = function(container, options) {
    28 PopcornReplacement.jwplayer = function(container, options) {
    29   Popcorn._container = container.slice(1); //eschew the '#'
    29   PopcornReplacement._container = container.slice(1); //eschew the '#'
    30   options.events = {
    30   options.events = {
    31       onReady: Popcorn.__initApi,
    31       onReady: PopcornReplacement.__initApi,
    32       onTime: Popcorn.__timeHandler,
    32       onTime: PopcornReplacement.__timeHandler,
    33       onSeek: Popcorn.__seekHandler }
    33       onSeek: PopcornReplacement.__seekHandler }
    34     
    34     
    35   jwplayer(Popcorn._container).setup(options);
    35   jwplayer(PopcornReplacement._container).setup(options);
    36   Popcorn.media.duration = options.duration;
    36   PopcornReplacement.media.duration = options.duration;
    37   return Popcorn;
    37   return PopcornReplacement;
    38 };
    38 };
    39 
    39 
    40 Popcorn.currentTime = function(time) {
    40 PopcornReplacement.currentTime = function(time) {
    41   if (typeof(time) === "undefined") {
    41   if (typeof(time) === "undefined") {
    42       return jwplayer(Popcorn._container).getPosition();            
    42       return jwplayer(PopcornReplacement._container).getPosition();            
    43   } else {
    43   } else {
    44      var currentTime = +time;
    44      var currentTime = +time;
    45      jwplayer( Popcorn._container ).seek( currentTime );
    45      jwplayer( PopcornReplacement._container ).seek( currentTime );
    46      return jwplayer(Popcorn._container).getPosition();            
    46      return jwplayer(PopcornReplacement._container).getPosition();            
    47   }
    47   }
    48 };
    48 };
    49 
    49 
    50 Popcorn.play = function() {
    50 PopcornReplacement.play = function() {
    51       Popcorn.media.paused = false;
    51       PopcornReplacement.media.paused = false;
    52 //      Popcorn.trigger("play");
    52 //      PopcornReplacement.trigger("play");
    53 //      Popcorn.trigger("playing");
    53 //      PopcornReplacement.trigger("playing");
    54       jwplayer( Popcorn._container ).play();
    54       jwplayer( PopcornReplacement._container ).play();
    55 };
    55 };
    56     
    56     
    57 Popcorn.pause = function() {
    57 PopcornReplacement.pause = function() {
    58       if ( !Popcorn.media.paused ) {
    58       if ( !PopcornReplacement.media.paused ) {
    59         Popcorn.media.paused = true;
    59         PopcornReplacement.media.paused = true;
    60         Popcorn.trigger( "pause" );
    60         PopcornReplacement.trigger( "pause" );
    61         jwplayer( Popcorn._container ).pause();
    61         jwplayer( PopcornReplacement._container ).pause();
    62       }
    62       }
    63 };
    63 };
    64 
    64 
    65 Popcorn.muted = function(val) {
    65 PopcornReplacement.muted = function(val) {
    66   if (typeof(val) !== "undefined") {
    66   if (typeof(val) !== "undefined") {
    67 
    67 
    68     if (jwplayer(Popcorn._container).getMute() !== val) {
    68     if (jwplayer(PopcornReplacement._container).getMute() !== val) {
    69       if (val) {
    69       if (val) {
    70         jwplayer(Popcorn._container).setMute(true);
    70         jwplayer(PopcornReplacement._container).setMute(true);
    71       } else {
    71       } else {
    72         jwplayer( Popcorn._container ).setMute(false);
    72         jwplayer( PopcornReplacement._container ).setMute(false);
    73       }
    73       }
    74 
    74 
    75       Popcorn.trigger( "volumechange" );
    75       PopcornReplacement.trigger( "volumechange" );
    76     }
    76     }
    77     
    77     
    78     return jwplayer( Popcorn._container ).getMute();
    78     return jwplayer( PopcornReplacement._container ).getMute();
    79   } else {
    79   } else {
    80     return jwplayer( Popcorn._container ).getMute();
    80     return jwplayer( PopcornReplacement._container ).getMute();
    81   }
    81   }
    82 };
    82 };
    83 
    83 
    84 Popcorn.mute = Popcorn.muted;
    84 PopcornReplacement.mute = PopcornReplacement.muted;
    85 
    85 
    86 Popcorn.__codes = [];
    86 PopcornReplacement.__codes = [];
    87 Popcorn.code = function(options) {
    87 PopcornReplacement.code = function(options) {
    88   Popcorn.__codes.push(options);
    88   PopcornReplacement.__codes.push(options);
    89   return Popcorn;
    89   return PopcornReplacement;
    90 };
    90 };
    91 
    91 
    92 Popcorn.__runCode = function() {
    92 PopcornReplacement.__runCode = function() {
    93   var currentTime = jwplayer(Popcorn._container).getPosition();
    93   var currentTime = jwplayer(PopcornReplacement._container).getPosition();
    94   var i = 0;
    94   var i = 0;
    95   for(i = 0; i < Popcorn.__codes.length; i++) {
    95   for(i = 0; i < PopcornReplacement.__codes.length; i++) {
    96     var c = Popcorn.__codes[i];
    96     var c = 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 Popcorn.__timeHandler = function(event) {
   112 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 < Popcorn.__codes.length; i++) {
   116   for(i = 0; i < PopcornReplacement.__codes.length; i++) {
   117      var c = Popcorn.__codes[i];
   117      var c = 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   Popcorn.trigger("timeupdate");
   131   PopcornReplacement.trigger("timeupdate");
   132 };
   132 };
   133 
   133 
   134 Popcorn.__seekHandler = function(event) { 
   134 PopcornReplacement.__seekHandler = function(event) { 
   135   var i = 0;
   135   var i = 0;
   136   for(i = 0; i < Popcorn.__codes.length; i++) {
   136   for(i = 0; i < PopcornReplacement.__codes.length; i++) {
   137      var c = Popcorn.__codes[i];
   137      var c = 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   Popcorn.trigger("timeupdate");
   151   PopcornReplacement.trigger("timeupdate");
   152 }
   152 }
   153 
   153 
   154 
   154 
   155 Popcorn.roundTime = function() {
   155 PopcornReplacement.roundTime = function() {
   156   var currentTime = Popcorn.currentTime();
   156   var currentTime = PopcornReplacement.currentTime();
   157   return Math.round(currentTime);
   157   return Math.round(currentTime);
   158 };
   158 };