| author | hamidouk |
| Tue, 10 Jan 2012 14:38:58 +0100 | |
| branch | popcorn-port |
| changeset 604 | cc2208986a4d |
| parent 596 | a0d2c5b96bee |
| child 605 | e1a6f73038b4 |
| permissions | -rw-r--r-- |
| 387 | 1 |
/* wrapper that simulates popcorn.js because |
2 |
popcorn is a bit unstable at the time */ |
|
| 388 | 3 |
|
|
445
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
4 |
IriSP.PopcornReplacement = { |
| 604 | 5 |
msgPump : {} /* used by jquery to receive and send messages */, |
6 |
__fake_currentTime : 0, /* when the stream is too slow, some events which depend |
|
7 |
on event "seeked" react to quickly and pickup the wrong currentTime. |
|
8 |
That's why we store the future currentTime in a var until the player |
|
9 |
has finished seeking */ |
|
10 |
__faking : false |
|
|
445
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
11 |
}; |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
12 |
|
| 442 | 13 |
IriSP.PopcornReplacement.media = { |
| 448 | 14 |
"paused": true, |
15 |
"muted": false |
|
| 442 | 16 |
}; |
| 390 | 17 |
|
| 431 | 18 |
IriSP.PopcornReplacement.listen = function(msg, callback) { |
|
445
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
19 |
// IriSP.jQuery(IriSP.PopcornReplacement.msgPump).bind(msg, function(event, rest) { callback(rest); }); |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
20 |
if (!IriSP.PopcornReplacement.msgPump.hasOwnProperty(msg)) |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
21 |
IriSP.PopcornReplacement.msgPump[msg] = []; |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
22 |
|
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
23 |
IriSP.PopcornReplacement.msgPump[msg].push(callback); |
| 388 | 24 |
}; |
25 |
||
| 431 | 26 |
IriSP.PopcornReplacement.trigger = function(msg, params) { |
|
445
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
27 |
// IriSP.jQuery(IriSP.PopcornReplacement.msgPump).trigger(msg, params); |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
28 |
|
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
29 |
if (!IriSP.PopcornReplacement.msgPump.hasOwnProperty(msg)) |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
30 |
return; |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
31 |
|
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
32 |
var d = IriSP.PopcornReplacement.msgPump[msg]; |
| 596 | 33 |
|
| 586 | 34 |
for(var i = 0; i < d.length; i++) { |
35 |
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
|
36 |
} |
|
c2eac11a9053
wrote our own pubsub system because of a bug in the jquery one
hamidouk
parents:
442
diff
changeset
|
37 |
|
| 388 | 38 |
}; |
39 |
||
| 431 | 40 |
IriSP.PopcornReplacement.guid = function(prefix) { |
| 388 | 41 |
var str = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
42 |
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); |
|
43 |
return v.toString(16); |
|
44 |
}); |
|
45 |
||
46 |
return prefix + str; |
|
47 |
}; |
|
48 |
||
| 431 | 49 |
IriSP.PopcornReplacement.__initApi = function() { |
|
463
7ef123b2410f
trigger a bunch of signals for popcorn compatibility.
hamidouk
parents:
458
diff
changeset
|
50 |
IriSP.PopcornReplacement.trigger("loadedmetadata"); // we've done more than loading metadata of course, |
|
7ef123b2410f
trigger a bunch of signals for popcorn compatibility.
hamidouk
parents:
458
diff
changeset
|
51 |
// but popcorn doesn't need to know more. |
| 458 | 52 |
IriSP.PopcornReplacement.media.muted = jwplayer(IriSP.PopcornReplacement._container).getMute(); |
| 578 | 53 |
|
54 |
/* some programmed segments are supposed to be run at the beginning */ |
|
55 |
var i = 0; |
|
56 |
for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) { |
|
57 |
var c = IriSP.PopcornReplacement.__codes[i]; |
|
58 |
if (0 == c.start) { |
|
59 |
c.onStart(); |
|
60 |
} |
|
61 |
|
|
62 |
if (0 == c.end) { |
|
63 |
c.onEnd(); |
|
64 |
} |
|
65 |
} |
|
| 388 | 66 |
}; |
67 |
||
| 431 | 68 |
IriSP.PopcornReplacement.jwplayer = function(container, options) { |
69 |
IriSP.PopcornReplacement._container = container.slice(1); //eschew the '#' |
|
| 388 | 70 |
options.events = { |
| 431 | 71 |
onReady: IriSP.PopcornReplacement.__initApi, |
72 |
onTime: IriSP.PopcornReplacement.__timeHandler, |
|
| 442 | 73 |
onPlay: IriSP.PopcornReplacement.__playHandler, |
74 |
onPause: IriSP.PopcornReplacement.__pauseHandler, |
|
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
75 |
onSeek: IriSP.PopcornReplacement.__seekHandler |
|
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
76 |
} |
| 388 | 77 |
|
| 431 | 78 |
jwplayer(IriSP.PopcornReplacement._container).setup(options); |
79 |
IriSP.PopcornReplacement.media.duration = options.duration; |
|
80 |
return IriSP.PopcornReplacement; |
|
| 388 | 81 |
}; |
82 |
||
| 431 | 83 |
IriSP.PopcornReplacement.currentTime = function(time) { |
| 388 | 84 |
if (typeof(time) === "undefined") { |
| 604 | 85 |
if (IriSP.PopcornReplacement.__faking === true) { |
86 |
return IriSP.PopcornReplacement.__fake_currentTime; |
|
87 |
} |
|
88 |
|
|
| 431 | 89 |
return jwplayer(IriSP.PopcornReplacement._container).getPosition(); |
| 388 | 90 |
} else { |
91 |
var currentTime = +time; |
|
| 431 | 92 |
jwplayer( IriSP.PopcornReplacement._container ).seek( currentTime ); |
| 604 | 93 |
IriSP.PopcornReplacement.__fake_currentTime = currentTime; |
|
463
7ef123b2410f
trigger a bunch of signals for popcorn compatibility.
hamidouk
parents:
458
diff
changeset
|
94 |
IriSP.PopcornReplacement.trigger("seeked"); |
| 604 | 95 |
//return jwplayer(IriSP.PopcornReplacement._container).getPosition(); |
96 |
return currentTime; |
|
| 388 | 97 |
} |
98 |
}; |
|
99 |
||
| 431 | 100 |
IriSP.PopcornReplacement.play = function() { |
101 |
IriSP.PopcornReplacement.media.paused = false; |
|
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
102 |
IriSP.PopcornReplacement.trigger("play"); |
| 431 | 103 |
// IriSP.PopcornReplacement.trigger("playing"); |
104 |
jwplayer( IriSP.PopcornReplacement._container ).play(); |
|
| 388 | 105 |
}; |
106 |
|
|
| 431 | 107 |
IriSP.PopcornReplacement.pause = function() { |
108 |
if ( !IriSP.PopcornReplacement.media.paused ) { |
|
109 |
IriSP.PopcornReplacement.media.paused = true; |
|
110 |
IriSP.PopcornReplacement.trigger( "pause" ); |
|
111 |
jwplayer( IriSP.PopcornReplacement._container ).pause(); |
|
| 388 | 112 |
} |
113 |
}; |
|
114 |
||
| 431 | 115 |
IriSP.PopcornReplacement.muted = function(val) { |
| 388 | 116 |
if (typeof(val) !== "undefined") { |
| 390 | 117 |
|
| 431 | 118 |
if (jwplayer(IriSP.PopcornReplacement._container).getMute() !== val) { |
| 390 | 119 |
if (val) { |
| 431 | 120 |
jwplayer(IriSP.PopcornReplacement._container).setMute(true); |
| 448 | 121 |
IriSP.PopcornReplacement.media.muted = true; |
| 388 | 122 |
} else { |
| 431 | 123 |
jwplayer( IriSP.PopcornReplacement._container ).setMute(false); |
| 448 | 124 |
IriSP.PopcornReplacement.media.muted = false; |
| 388 | 125 |
} |
126 |
||
| 431 | 127 |
IriSP.PopcornReplacement.trigger( "volumechange" ); |
| 388 | 128 |
} |
129 |
|
|
| 431 | 130 |
return jwplayer( IriSP.PopcornReplacement._container ).getMute(); |
| 388 | 131 |
} else { |
| 431 | 132 |
return jwplayer( IriSP.PopcornReplacement._container ).getMute(); |
| 388 | 133 |
} |
| 390 | 134 |
}; |
135 |
||
| 431 | 136 |
IriSP.PopcornReplacement.mute = IriSP.PopcornReplacement.muted; |
| 394 | 137 |
|
| 431 | 138 |
IriSP.PopcornReplacement.__codes = []; |
139 |
IriSP.PopcornReplacement.code = function(options) { |
|
140 |
IriSP.PopcornReplacement.__codes.push(options); |
|
141 |
return IriSP.PopcornReplacement; |
|
| 390 | 142 |
}; |
143 |
||
| 394 | 144 |
/* called everytime the player updates itself |
145 |
(onTime event) |
|
146 |
*/ |
|
147 |
||
| 431 | 148 |
IriSP.PopcornReplacement.__timeHandler = function(event) { |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
149 |
var pos = event.position; |
| 578 | 150 |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
151 |
var i = 0; |
| 431 | 152 |
for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) { |
153 |
var c = IriSP.PopcornReplacement.__codes[i]; |
|
| 580 | 154 |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
155 |
if (pos >= c.start && pos < c.end && |
| 580 | 156 |
pos - 1 <= c.start) { |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
157 |
c.onStart(); |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
158 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
159 |
|
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
160 |
if (pos > c.start && pos > c.end && |
| 580 | 161 |
pos - 1 <= c.end) { |
| 578 | 162 |
c.onEnd(); |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
163 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
164 |
|
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
165 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
166 |
|
| 431 | 167 |
IriSP.PopcornReplacement.trigger("timeupdate"); |
| 390 | 168 |
}; |
| 394 | 169 |
|
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
170 |
IriSP.PopcornReplacement.__seekHandler = function(event) { |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
171 |
var i = 0; |
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
172 |
|
| 431 | 173 |
for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) { |
174 |
var c = IriSP.PopcornReplacement.__codes[i]; |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
175 |
|
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
176 |
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
|
177 |
c.onEnd(); |
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
178 |
} |
|
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
179 |
} |
| 580 | 180 |
|
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
181 |
for(i = 0; i < IriSP.PopcornReplacement.__codes.length; i++) { |
|
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
182 |
var c = IriSP.PopcornReplacement.__codes[i]; |
|
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
183 |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
184 |
if (typeof(event.offset) === "undefined") |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
185 |
event.offset = 0; |
|
475
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
186 |
|
|
5c254621cd9c
fixed popcorn replacement bug where onEnd would be called after onStart.
hamidouk
parents:
463
diff
changeset
|
187 |
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
|
188 |
c.onStart(); |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
189 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
190 |
|
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
191 |
} |
|
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
192 |
|
| 431 | 193 |
IriSP.PopcornReplacement.trigger("timeupdate"); |
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
194 |
}; |
|
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
195 |
|
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
196 |
|
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
197 |
IriSP.PopcornReplacement.__playHandler = function(event) { |
| 604 | 198 |
if (IriSP.PopcornReplacement.__faking === true) |
199 |
IriSP.PopcornReplacement.__faking = false; |
|
200 |
|
|
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
201 |
IriSP.PopcornReplacement.media.paused = false; |
| 442 | 202 |
IriSP.PopcornReplacement.trigger("play"); |
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
203 |
}; |
|
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
204 |
|
|
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
205 |
IriSP.PopcornReplacement.__pauseHandler = function(event) { |
|
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
206 |
IriSP.PopcornReplacement.media.paused = true; |
| 442 | 207 |
IriSP.PopcornReplacement.trigger("pause"); |
|
441
99b7c5192330
fixed horrible bug involving jQuery message passing.
hamidouk
parents:
431
diff
changeset
|
208 |
}; |
|
417
bae7c50704d7
fixed code() function. The Popcorn workaround now works correctly.
hamidouk
parents:
394
diff
changeset
|
209 |
|
| 431 | 210 |
IriSP.PopcornReplacement.roundTime = function() { |
211 |
var currentTime = IriSP.PopcornReplacement.currentTime(); |
|
| 394 | 212 |
return Math.round(currentTime); |
213 |
}; |