# HG changeset patch # User hamidouk # Date 1327422601 -3600 # Node ID f1225d38c150695fe58e04fa944abe7232356aac # Parent a773f117d2e7c0c7afff5edc381f6c34d0904701 new, extendable api. diff -r a773f117d2e7 -r f1225d38c150 src/js/pop.js --- a/src/js/pop.js Tue Jan 24 17:29:45 2012 +0100 +++ b/src/js/pop.js Tue Jan 24 17:30:01 2012 +0100 @@ -1,27 +1,36 @@ /* wrapper that simulates popcorn.js because popcorn is a bit unstable at the time */ -IriSP.PopcornReplacement = { +IriSP.PopcornReplacement = { }; - -/** instantiate a new popcorn-compatible player - @param playerFns an object which contains a number of - methods: init, play, pause, getMute, setMute used by - our wrapper. +/** base class for our popcorn-compatible players. */ -IriSP.PopcornReplacement.player = function(playerFns) { - this.playerFns = playerFns; +IriSP.PopcornReplacement.player = function(container, options) { + /* the jwplayer calls the callbacks in the global space so we need to + preserve them using IriSP.wrap */ + this.callbacks = { + onReady: IriSP.wrap(this, this.__initApi), + onTime: IriSP.wrap(this, this.__timeHandler), + onPlay: IriSP.wrap(this, this.__playHandler), + onPause: IriSP.wrap(this, this.__pauseHandler), + onSeek: IriSP.wrap(this, this.__seekHandler) + }; + this.media = { "paused": true, "muted": false }; + + this.container = container.slice(1); //eschew the '#' this.msgPump = {}; /* dictionnary used to receive and send messages */ + this.__codes = []; /* used to schedule the execution of a piece of code in + a segment (similar to the popcorn.code plugin). */ + }; IriSP.PopcornReplacement.player.prototype.listen = function(msg, callback) { -// IriSP.jQuery(IriSP.PopcornReplacement.msgPump).bind(msg, function(event, rest) { callback(rest); }); if (!this.msgPump.hasOwnProperty(msg)) this.msgPump[msg] = []; @@ -29,8 +38,6 @@ }; IriSP.PopcornReplacement.player.prototype.trigger = function(msg, params) { -// IriSP.jQuery(IriSP.PopcornReplacement.msgPump).trigger(msg, params); - if (!this.msgPump.hasOwnProperty(msg)) return; @@ -42,7 +49,7 @@ }; -IriSP.PopcornReplacement.guid = function(prefix) { +IriSP.PopcornReplacement.player.prototype.guid = function(prefix) { var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); @@ -51,15 +58,17 @@ return prefix + str; }; -IriSP.PopcornReplacement.__initApi = function() { - IriSP.PopcornReplacement.trigger("loadedmetadata"); // we've done more than loading metadata of course, +/** init the api after that flash player has been setup - called by the callback + defined by the embedded flash player +*/ +IriSP.PopcornReplacement.player.prototype.__initApi = function() { + this.trigger("loadedmetadata"); // we've done more than loading metadata of course, // but popcorn doesn't need to know more. - IriSP.PopcornReplacement.media.muted = jwplayer(IriSP.PopcornReplacement._container).getMute(); - + this.media.muted = this.playerFns.getMute(); /* some programmed segments are supposed to be run at the beginning */ var i = 0; - for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) { - var c = IriSP.PopcornReplacement.__codes[i]; + for(i = 0; i < this.__codes.length; i++) { + var c = this.__codes[i]; if (0 == c.start) { c.onStart(); } @@ -70,6 +79,7 @@ } }; +/* IriSP.PopcornReplacement.jwplayer = function(container, options) { IriSP.PopcornReplacement._container = container.slice(1); //eschew the '#' options.events = { @@ -84,73 +94,73 @@ IriSP.PopcornReplacement.media.duration = options.duration; return IriSP.PopcornReplacement; }; +*/ -IriSP.PopcornReplacement.currentTime = function(time) { +IriSP.PopcornReplacement.player.prototype.currentTime = function(time) { if (typeof(time) === "undefined") { - return jwplayer(IriSP.PopcornReplacement._container).getPosition(); + return this.playerFns.getPosition(); } else { var currentTime = +time; - jwplayer( IriSP.PopcornReplacement._container ).seek( currentTime ); - IriSP.PopcornReplacement.__delay_seek_signal = true; - IriSP.PopcornReplacement.trigger("seeked"); + this.playerFns.seek( currentTime ); + this.__delay_seek_signal = true; /* FIXME : useless ? */ + this.trigger("seeked"); return currentTime; } }; -IriSP.PopcornReplacement.play = function() { - IriSP.PopcornReplacement.media.paused = false; - IriSP.PopcornReplacement.trigger("play"); -// IriSP.PopcornReplacement.trigger("playing"); - jwplayer( IriSP.PopcornReplacement._container ).play(); +IriSP.PopcornReplacement.player.prototype.play = function() { + this.media.paused = false; + this.trigger("play"); + //IriSP.PopcornReplacement.trigger("playing"); + this.playerFns.play(); }; -IriSP.PopcornReplacement.pause = function() { - if ( !IriSP.PopcornReplacement.media.paused ) { - IriSP.PopcornReplacement.media.paused = true; - IriSP.PopcornReplacement.trigger( "pause" ); - jwplayer( IriSP.PopcornReplacement._container ).pause(); - } -}; - -IriSP.PopcornReplacement.muted = function(val) { - if (typeof(val) !== "undefined") { - - if (jwplayer(IriSP.PopcornReplacement._container).getMute() !== val) { - if (val) { - jwplayer(IriSP.PopcornReplacement._container).setMute(true); - IriSP.PopcornReplacement.media.muted = true; - } else { - jwplayer( IriSP.PopcornReplacement._container ).setMute(false); - IriSP.PopcornReplacement.media.muted = false; - } - - IriSP.PopcornReplacement.trigger( "volumechange" ); - } - - return jwplayer( IriSP.PopcornReplacement._container ).getMute(); - } else { - return jwplayer( IriSP.PopcornReplacement._container ).getMute(); +IriSP.PopcornReplacement.player.prototype.pause = function() { + if ( !this.media.paused ) { + this.media.paused = true; + this.trigger( "pause" ); + this.playerFns.pause(); } }; -IriSP.PopcornReplacement.mute = IriSP.PopcornReplacement.muted; +IriSP.PopcornReplacement.player.prototype.muted = function(val) { + if (typeof(val) !== "undefined") { + + if (this.playerFns.getMute() !== val) { + if (val) { + this.playerFns.setMute(true); + this.media.muted = true; + } else { + this.playerFns.setMute(false); + this.media.muted = false; + } -IriSP.PopcornReplacement.__codes = []; -IriSP.PopcornReplacement.code = function(options) { - IriSP.PopcornReplacement.__codes.push(options); - return IriSP.PopcornReplacement; + this.trigger( "volumechange" ); + } + + return this.playerFns.getMute(); + } else { + return this.playerFns.getMute(); + } +}; + +IriSP.PopcornReplacement.player.prototype.mute = IriSP.PopcornReplacement.player.prototype.muted; + +IriSP.PopcornReplacement.player.prototype.code = function(options) { + this.__codes.push(options); + return this; }; /* called everytime the player updates itself (onTime event) */ -IriSP.PopcornReplacement.__timeHandler = function(event) { +IriSP.PopcornReplacement.timeHandler = function(event) { var pos = event.position; - + var i = 0; - for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) { - var c = IriSP.PopcornReplacement.__codes[i]; + for(i = 0; i < this.__codes.length; i++) { + var c = this.__codes[i]; if (pos >= c.start && pos < c.end && pos - 1 <= c.start) { @@ -164,22 +174,23 @@ } - IriSP.PopcornReplacement.trigger("timeupdate"); + this.trigger("timeupdate"); }; -IriSP.PopcornReplacement.__seekHandler = function(event) { +IriSP.PopcornReplacement.player.prototype.__timeHandler = IriSP.PopcornReplacement.timeHandler; +IriSP.PopcornReplacement.player.prototype.__seekHandler = function(event) { var i = 0; - - for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) { - var c = IriSP.PopcornReplacement.__codes[i]; + console.log(event); + for(i = 0; i < this.__codes.length; i++) { + var c = this.__codes[i]; if (event.position >= c.start && event.position < c.end) { c.onEnd(); } } - for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) { - var c = IriSP.PopcornReplacement.__codes[i]; + for(i = 0; i < this.__codes.length; i++) { + var c = this.__codes[i]; if (typeof(event.offset) === "undefined") event.offset = 0; @@ -190,22 +201,48 @@ } - IriSP.PopcornReplacement.trigger("timeupdate"); + this.trigger("timeupdate"); +}; + +IriSP.PopcornReplacement.player.prototype.__playHandler = function(event) { + this.media.paused = false; + this.trigger("play"); }; - -IriSP.PopcornReplacement.__playHandler = function(event) { - IriSP.PopcornReplacement.media.paused = false; - IriSP.PopcornReplacement.trigger("play"); +IriSP.PopcornReplacement.player.prototype.__pauseHandler = function(event) { + this.media.paused = true; + this.trigger("pause"); }; -IriSP.PopcornReplacement.__pauseHandler = function(event) { - IriSP.PopcornReplacement.media.paused = true; - IriSP.PopcornReplacement.trigger("pause"); -}; - -IriSP.PopcornReplacement.roundTime = function() { - var currentTime = IriSP.PopcornReplacement.currentTime(); +IriSP.PopcornReplacement.player.prototype.roundTime = function() { + var currentTime = this.currentTime(); return Math.round(currentTime); }; +/* To wrap a player the develop should create a new class derived from + the IriSP.PopcornReplacement.player and defining the correct functions */ + +/* Exemple with jwplayer */ +IriSP.PopcornReplacement.jwplayer = function(container, options) { + + /* appel du parent pour initialiser les structures communes à tous les players */ + IriSP.PopcornReplacement.player.call(this, container, options); + + this.media.duration = options.duration; /* optional */ + + /* Définition des fonctions de l'API - */ + this.playerFns = { + play: function() { return jwplayer(this.container).play(); }, + pause: function() { return jwplayer(this.container).pause(); }, + getPosition: function() { return jwplayer(this.container).getPosition(); }, + seek: function(pos) { return jwplayer(this.container).seek(pos); }, + getMute: function() { return jwplayer(this.container).getMute() }, + setMute: function(p) { return jwplayer(this.container).setMute(p); } + } + + options.events = this.callbacks; + + jwplayer(this.container).setup(options); +}; + +IriSP.PopcornReplacement.jwplayer.prototype = new IriSP.PopcornReplacement.player("", {});