| author | hamidouk |
| Wed, 01 Feb 2012 12:40:45 +0100 | |
| branch | nih-youtube |
| changeset 757 | 43e261bcd4ce |
| parent 750 | a1a3d09de1f4 |
| permissions | -rw-r--r-- |
| 387 | 1 |
/* wrapper that simulates popcorn.js because |
2 |
popcorn is a bit unstable at the time */ |
|
| 388 | 3 |
|
| 702 | 4 |
IriSP.PopcornReplacement = { |
| 442 | 5 |
}; |
| 390 | 6 |
|
| 702 | 7 |
/** base class for our popcorn-compatible players. |
| 699 | 8 |
*/ |
| 702 | 9 |
IriSP.PopcornReplacement.player = function(container, options) { |
10 |
/* the jwplayer calls the callbacks in the global space so we need to |
|
11 |
preserve them using IriSP.wrap */ |
|
12 |
this.callbacks = { |
|
13 |
onReady: IriSP.wrap(this, this.__initApi), |
|
14 |
onTime: IriSP.wrap(this, this.__timeHandler), |
|
15 |
onPlay: IriSP.wrap(this, this.__playHandler), |
|
16 |
onPause: IriSP.wrap(this, this.__pauseHandler), |
|
17 |
onSeek: IriSP.wrap(this, this.__seekHandler) |
|
18 |
}; |
|
19 |
|
|
| 699 | 20 |
this.media = { |
21 |
"paused": true, |
|
22 |
"muted": false |
|
23 |
}; |
|
| 702 | 24 |
|
| 757 | 25 |
var id = container.split("#"); //eschew the '#' |
26 |
if (id.length === 1) |
|
27 |
this.container = id[0]; |
|
28 |
else if (id.length > 1) |
|
29 |
this.container = id[1]; |
|
| 699 | 30 |
|
31 |
this.msgPump = {}; /* dictionnary used to receive and send messages */ |
|
| 702 | 32 |
this.__codes = []; /* used to schedule the execution of a piece of code in |
33 |
a segment (similar to the popcorn.code plugin). */ |
|
34 |
|
|
| 388 | 35 |
}; |
36 |
||
| 699 | 37 |
IriSP.PopcornReplacement.player.prototype.listen = function(msg, callback) { |
38 |
if (!this.msgPump.hasOwnProperty(msg)) |
|
39 |
this.msgPump[msg] = []; |
|
40 |
||
41 |
this.msgPump[msg].push(callback); |
|
42 |
}; |
|
43 |
||
44 |
IriSP.PopcornReplacement.player.prototype.trigger = function(msg, params) { |
|
45 |
if (!this.msgPump.hasOwnProperty(msg)) |
|
|
445
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
46 |
return; |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
47 |
|
| 699 | 48 |
var d = this.msgPump[msg]; |
| 596 | 49 |
|
| 586 | 50 |
for(var i = 0; i < d.length; i++) { |
51 |
d[i].call(window, params); |
|
|
445
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
52 |
} |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
53 |
|
| 388 | 54 |
}; |
55 |
||
| 702 | 56 |
IriSP.PopcornReplacement.player.prototype.guid = function(prefix) { |
| 388 | 57 |
var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
58 |
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); |
|
59 |
return v.toString(16); |
|
60 |
}); |
|
61 |
||
62 |
return prefix + str; |
|
63 |
}; |
|
64 |
||
| 702 | 65 |
/** init the api after that flash player has been setup - called by the callback |
66 |
defined by the embedded flash player |
|
67 |
*/ |
|
68 |
IriSP.PopcornReplacement.player.prototype.__initApi = function() { |
|
69 |
this.trigger("loadedmetadata"); // we've done more than loading metadata of course, |
|
|
463
7ef123b2410f
trigger a bunch of signals for popcorn compatibility.
hamidouk
parents:
458
diff
changeset
|
70 |
// but popcorn doesn't need to know more. |
| 702 | 71 |
this.media.muted = this.playerFns.getMute(); |
| 578 | 72 |
/* some programmed segments are supposed to be run at the beginning */ |
73 |
var i = 0; |
|
| 702 | 74 |
for(i = 0; i < this.__codes.length; i++) { |
75 |
var c = this.__codes[i]; |
|
| 578 | 76 |
if (0 == c.start) { |
77 |
c.onStart(); |
|
78 |
} |
|
79 |
|
|
80 |
if (0 == c.end) { |
|
81 |
c.onEnd(); |
|
82 |
} |
|
83 |
} |
|
| 388 | 84 |
}; |
85 |
||
| 702 | 86 |
/* |
| 431 | 87 |
IriSP.PopcornReplacement.jwplayer = function(container, options) { |
88 |
IriSP.PopcornReplacement._container = container.slice(1); //eschew the '#' |
|
| 388 | 89 |
options.events = { |
| 431 | 90 |
onReady: IriSP.PopcornReplacement.__initApi, |
91 |
onTime: IriSP.PopcornReplacement.__timeHandler, |
|
| 442 | 92 |
onPlay: IriSP.PopcornReplacement.__playHandler, |
93 |
onPause: IriSP.PopcornReplacement.__pauseHandler, |
|
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
94 |
onSeek: IriSP.PopcornReplacement.__seekHandler |
|
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
95 |
} |
| 388 | 96 |
|
| 431 | 97 |
jwplayer(IriSP.PopcornReplacement._container).setup(options); |
98 |
IriSP.PopcornReplacement.media.duration = options.duration; |
|
99 |
return IriSP.PopcornReplacement; |
|
| 388 | 100 |
}; |
| 702 | 101 |
*/ |
| 388 | 102 |
|
| 702 | 103 |
IriSP.PopcornReplacement.player.prototype.currentTime = function(time) { |
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
604
diff
changeset
|
104 |
if (typeof(time) === "undefined") { |
| 702 | 105 |
return this.playerFns.getPosition(); |
| 388 | 106 |
} else { |
107 |
var currentTime = +time; |
|
| 723 | 108 |
this.playerFns.seek(currentTime); |
| 604 | 109 |
return currentTime; |
| 388 | 110 |
} |
111 |
}; |
|
112 |
||
| 702 | 113 |
IriSP.PopcornReplacement.player.prototype.play = function() { |
114 |
this.media.paused = false; |
|
115 |
this.trigger("play"); |
|
116 |
//IriSP.PopcornReplacement.trigger("playing"); |
|
117 |
this.playerFns.play(); |
|
| 388 | 118 |
}; |
119 |
|
|
| 702 | 120 |
IriSP.PopcornReplacement.player.prototype.pause = function() { |
121 |
if ( !this.media.paused ) { |
|
122 |
this.media.paused = true; |
|
123 |
this.trigger( "pause" ); |
|
124 |
this.playerFns.pause(); |
|
| 388 | 125 |
} |
| 390 | 126 |
}; |
127 |
||
| 702 | 128 |
IriSP.PopcornReplacement.player.prototype.muted = function(val) { |
129 |
if (typeof(val) !== "undefined") { |
|
130 |
||
131 |
if (this.playerFns.getMute() !== val) { |
|
132 |
if (val) { |
|
133 |
this.playerFns.setMute(true); |
|
134 |
this.media.muted = true; |
|
135 |
} else { |
|
136 |
this.playerFns.setMute(false); |
|
137 |
this.media.muted = false; |
|
138 |
} |
|
| 394 | 139 |
|
| 702 | 140 |
this.trigger( "volumechange" ); |
141 |
} |
|
142 |
|
|
143 |
return this.playerFns.getMute(); |
|
144 |
} else { |
|
145 |
return this.playerFns.getMute(); |
|
146 |
} |
|
147 |
}; |
|
148 |
||
149 |
IriSP.PopcornReplacement.player.prototype.mute = IriSP.PopcornReplacement.player.prototype.muted; |
|
150 |
||
151 |
IriSP.PopcornReplacement.player.prototype.code = function(options) { |
|
152 |
this.__codes.push(options); |
|
153 |
return this; |
|
| 390 | 154 |
}; |
155 |
||
| 394 | 156 |
/* called everytime the player updates itself |
157 |
(onTime event) |
|
158 |
*/ |
|
159 |
||
| 706 | 160 |
IriSP.PopcornReplacement.player.prototype.__timeHandler = function(event) { |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
161 |
var pos = event.position; |
| 702 | 162 |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
163 |
var i = 0; |
| 702 | 164 |
for(i = 0; i < this.__codes.length; i++) { |
165 |
var c = this.__codes[i]; |
|
| 580 | 166 |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
167 |
if (pos >= c.start && pos < c.end && |
| 580 | 168 |
pos - 1 <= c.start) { |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
169 |
c.onStart(); |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
170 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
171 |
|
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
172 |
if (pos > c.start && pos > c.end && |
| 580 | 173 |
pos - 1 <= c.end) { |
| 578 | 174 |
c.onEnd(); |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
175 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
176 |
|
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
177 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
178 |
|
| 702 | 179 |
this.trigger("timeupdate"); |
| 390 | 180 |
}; |
| 394 | 181 |
|
| 702 | 182 |
IriSP.PopcornReplacement.player.prototype.__seekHandler = function(event) { |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
183 |
var i = 0; |
| 723 | 184 |
|
| 702 | 185 |
for(i = 0; i < this.__codes.length; i++) { |
186 |
var c = this.__codes[i]; |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
187 |
|
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
188 |
if (event.position >= c.start && event.position < c.end) { |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
189 |
c.onEnd(); |
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
190 |
} |
|
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
191 |
} |
| 580 | 192 |
|
| 702 | 193 |
for(i = 0; i < this.__codes.length; i++) { |
194 |
var c = this.__codes[i]; |
|
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
195 |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
196 |
if (typeof(event.offset) === "undefined") |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
197 |
event.offset = 0; |
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
198 |
|
|
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
199 |
if (event.offset >= c.start && event.offset < c.end) { |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
200 |
c.onStart(); |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
201 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
202 |
|
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
203 |
} |
| 723 | 204 |
|
| 750 | 205 |
/* this signal sends as an extra argument the position in the video. |
206 |
As far as I know, this argument is not provided by popcorn */ |
|
| 749 | 207 |
this.trigger("seeked", event.offset); |
| 702 | 208 |
}; |
209 |
||
210 |
IriSP.PopcornReplacement.player.prototype.__playHandler = function(event) { |
|
211 |
this.media.paused = false; |
|
212 |
this.trigger("play"); |
|
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
213 |
}; |
|
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
214 |
|
| 702 | 215 |
IriSP.PopcornReplacement.player.prototype.__pauseHandler = function(event) { |
216 |
this.media.paused = true; |
|
217 |
this.trigger("pause"); |
|
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
218 |
}; |
|
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
219 |
|
| 702 | 220 |
IriSP.PopcornReplacement.player.prototype.roundTime = function() { |
221 |
var currentTime = this.currentTime(); |
|
| 394 | 222 |
return Math.round(currentTime); |
|
711
323205a7bd39
separated jwplayer code from pop.js and put it into its own folder, players/.
hamidouk
parents:
706
diff
changeset
|
223 |
}; |