src/js/players/player.youtube.js
author ymh <ymh.work@gmail.com>
Fri, 06 Apr 2012 18:01:12 +0200
branchnih-youtube
changeset 849 85f06cc23bfd
parent 757 43e261bcd4ce
permissions -rw-r--r--
close branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     1
/* To wrap a player the develop should create a new class derived from 
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     2
   the IriSP.PopcornReplacement.player and defining the correct functions */
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     3
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     4
/** youtube player wrapper */
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     5
IriSP.PopcornReplacement.youtube = function(container, options) {
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     6
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     7
  /* appel du parent pour initialiser les structures communes à tous les players */
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     8
  IriSP.PopcornReplacement.player.call(this, container, options);
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
     9
  
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    10
  this.media.duration = options.duration; /* optional */
757
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    11
  var _this = this;
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    12
  
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    13
  /* Définition des fonctions de l'API -  */
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    14
  this.playerFns = {
757
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    15
    play: function() { return _this.player.playVideo(); },
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    16
    pause: function() { return _this.player.pauseVideo(); },
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    17
    getPosition: function() { console.log("ho"); return _this.player.getCurrentTime(); },
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    18
    seek: function(pos) { return _this.player.seekTo(pos, true); },
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    19
    getMute: function() { return _this.player.isMuted(); },
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    20
    setMute: function(p) { return _this.player.mute(p); }
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    21
  }
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    22
757
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    23
  window.onYouTubePlayerReady = IriSP.wrap(this, this.ready);
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    24
  
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    25
  options.events = this.callbacks;
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    26
  var params = { allowScriptAccess: "always" };
756
f971fb0f2a3e more work on the youtube rewrite front.
hamidouk
parents: 755
diff changeset
    27
  var atts = { id: container };
f971fb0f2a3e more work on the youtube rewrite front.
hamidouk
parents: 755
diff changeset
    28
  swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&version=3",
f971fb0f2a3e more work on the youtube rewrite front.
hamidouk
parents: 755
diff changeset
    29
                     container, options.width, options.height, "8", null, null, params, atts);
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    30
756
f971fb0f2a3e more work on the youtube rewrite front.
hamidouk
parents: 755
diff changeset
    31
  
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    32
};
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    33
757
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    34
IriSP.PopcornReplacement.youtube.prototype = new IriSP.PopcornReplacement.player("", {});
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    35
757
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    36
IriSP.PopcornReplacement.youtube.prototype.ready = function() {
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    37
  // save the player dom object.
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    38
  this.player = IriSP.jQuery("#" + this.container).get(0);
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    39
  // crap - youtube player expects a string describing the function to call.
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    40
  this.player.addEventListener("onStateChange", "IriSP.PopcornReplacement.youtube.prototype.stateHandler");
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    41
  this.player.loadVideoByUrl("http://www.youtube.com/v/ucc58jIasI8?version=3");
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    42
  
756
f971fb0f2a3e more work on the youtube rewrite front.
hamidouk
parents: 755
diff changeset
    43
};
f971fb0f2a3e more work on the youtube rewrite front.
hamidouk
parents: 755
diff changeset
    44
757
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    45
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    46
/* we've got to store the previous state in the prototype because, well
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    47
   the youtube player is crap */
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    48
IriSP.PopcornReplacement.youtube.prototype.previousState = -1;
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    49
757
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    50
IriSP.PopcornReplacement.youtube.prototype.stateHandler = function(state) {
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    51
  if (state === 1) // playing { 
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    52
    /* we're already playing */
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    53
    
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    54
    if (IriSP.PopcornReplacement.youtube.prototype.previousState === state) {
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    55
      var time = this.player.getCurrentTime();
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    56
      console.log(time);
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    57
      this.callbacks.onTime({position: time});
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    58
    } else { /* we're entering the playing state */
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    59
      this.callbacks.onPlay();
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    60
    }
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    61
  else if (state === 2) // paused
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    62
    this.callbacks.onPause();
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    63
  else if (state === 3) // buffering
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    64
    this.callbacks.onSeek();
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    65
  
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    66
  else if (state === 5) /* video ready to play */
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    67
    this.callbacks.onReady();
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    68
  
43e261bcd4ce more work on the youtube player.
hamidouk
parents: 756
diff changeset
    69
  IriSP.PopcornReplacement.youtube.prototype.previousState = state;
755
d60401f66984 first version of the youtube compatible player.
hamidouk
parents:
diff changeset
    70
};