more work on the youtube player. nih-youtube
authorhamidouk
Wed, 01 Feb 2012 12:40:45 +0100
branchnih-youtube
changeset 757 43e261bcd4ce
parent 756 f971fb0f2a3e
child 849 85f06cc23bfd
more work on the youtube player.
src/js/init.js
src/js/players/player.youtube.js
src/js/pop.js
--- a/src/js/init.js	Tue Jan 31 17:22:10 2012 +0100
+++ b/src/js/init.js	Wed Feb 01 12:40:45 2012 +0100
@@ -146,7 +146,9 @@
 
 /** configure modules. @see configureWidgets */
 IriSP.configureModules = function (popcornInstance, modulesList) {
- 
+  if (IriSP.null_or_undefined(modulesList))
+    return;
+  
   var serialFactory = new IriSP.SerializerFactory(IriSP.__dataloader);
   var ret_modules = [];
   var index;
--- a/src/js/players/player.youtube.js	Tue Jan 31 17:22:10 2012 +0100
+++ b/src/js/players/player.youtube.js	Wed Feb 01 12:40:45 2012 +0100
@@ -8,18 +8,19 @@
   IriSP.PopcornReplacement.player.call(this, container, options);
   
   this.media.duration = options.duration; /* optional */
+  var _this = this;
   
   /* 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); }
+    play: function() { return _this.player.playVideo(); },
+    pause: function() { return _this.player.pauseVideo(); },
+    getPosition: function() { console.log("ho"); return _this.player.getCurrentTime(); },
+    seek: function(pos) { return _this.player.seekTo(pos, true); },
+    getMute: function() { return _this.player.isMuted(); },
+    setMute: function(p) { return _this.player.mute(p); }
   }
 
-  onYoutubePlayerReady = IriSP.wrap(this, this.ready);
+  window.onYouTubePlayerReady = IriSP.wrap(this, this.ready);
   
   options.events = this.callbacks;
   var params = { allowScriptAccess: "always" };
@@ -30,15 +31,40 @@
   
 };
 
-IriSP.PopcornReplacement.jwplayer.prototype = new IriSP.PopcornReplacement.player("", {});
+IriSP.PopcornReplacement.youtube.prototype = new IriSP.PopcornReplacement.player("", {});
 
-IriSP.PopcornReplacement.jwplayer.prototype.ready = function(playerId) {
-  console.log("called");
-  this.player = IriSP.jQuery("#" + playerId).get(0);
-  this.player.loadVideoByUrl("http://www.youtube.com/v/QH2-TGUlwu4?version=3");
-  this.player.playVideo();
+IriSP.PopcornReplacement.youtube.prototype.ready = function() {
+  // save the player dom object.
+  this.player = IriSP.jQuery("#" + this.container).get(0);
+  // crap - youtube player expects a string describing the function to call.
+  this.player.addEventListener("onStateChange", "IriSP.PopcornReplacement.youtube.prototype.stateHandler");
+  this.player.loadVideoByUrl("http://www.youtube.com/v/ucc58jIasI8?version=3");
+  
 };
 
-IriSP.PopcornReplacement.jwplayer.prototype.stateHandler = function(state) {
+
+/* we've got to store the previous state in the prototype because, well
+   the youtube player is crap */
+IriSP.PopcornReplacement.youtube.prototype.previousState = -1;
 
+IriSP.PopcornReplacement.youtube.prototype.stateHandler = function(state) {
+  if (state === 1) // playing { 
+    /* we're already playing */
+    
+    if (IriSP.PopcornReplacement.youtube.prototype.previousState === state) {
+      var time = this.player.getCurrentTime();
+      console.log(time);
+      this.callbacks.onTime({position: time});
+    } else { /* we're entering the playing state */
+      this.callbacks.onPlay();
+    }
+  else if (state === 2) // paused
+    this.callbacks.onPause();
+  else if (state === 3) // buffering
+    this.callbacks.onSeek();
+  
+  else if (state === 5) /* video ready to play */
+    this.callbacks.onReady();
+  
+  IriSP.PopcornReplacement.youtube.prototype.previousState = state;
 };
\ No newline at end of file
--- a/src/js/pop.js	Tue Jan 31 17:22:10 2012 +0100
+++ b/src/js/pop.js	Wed Feb 01 12:40:45 2012 +0100
@@ -22,7 +22,11 @@
     "muted": false
   };
     
-  this.container = container.slice(1); //eschew the '#'
+  var id = container.split("#"); //eschew the '#'
+  if (id.length === 1)
+    this.container = id[0];
+  else if (id.length > 1)
+    this.container = id[1];
   
   this.msgPump = {}; /* dictionnary used to receive and send messages */
   this.__codes = []; /* used to schedule the execution of a piece of code in