-
Notifications
You must be signed in to change notification settings - Fork 22
/
jquery.jwplayer.js
374 lines (341 loc) · 10 KB
/
jquery.jwplayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
;(function ($) {
/*!
jQuery.jwPlayer
by Keith C. Ivey, keith@iveys.org
Copyright 2010, Smokescreen Corporation
Dual licensed under the MIT and GPL licenses
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
Version 0.115 (2010-08-18)
*/
var pluginName = 'jwPlayer';
if (!window.console) { console = {}; } // prevent errors from console.log calls
if (!console.log) { console.log = $.noop; }
var eventType = {
ITEM: 'Controller',
MUTE: 'Controller',
PLAY: 'Controller',
PLAYLIST: 'Controller',
RESIZE: 'Controller',
SEEK: 'Controller',
STOP: 'Controller',
VOLUME: 'Controller',
BUFFER: 'Model',
ERROR: 'Model',
LOADED: 'Model',
META: 'Model',
STATE: 'Model',
TIME: 'Model',
FULLSCREEN: 'View',
LINK: 'View',
LOAD: 'View',
NEXT: 'View',
PREV: 'View',
REDRAW: 'View'
};
// Properties to be set in object data when listeners are fired
var listenerProperties = {
ITEM: {index: 'item'},
STATE: {newstate: 'state'},
TIME: {position: 'position', duration: 'duration'},
VOLUME: {volume: 'volume'}
};
var flashvarNames = [
'author',
'autostart',
'backcolor',
'bufferlength',
'config',
'controlbar',
'date',
'debug',
'description',
'displaytitle',
'dock',
'duration',
'file',
'frontcolor',
'fullscreen',
'icons',
'image',
'item',
'lightcolor',
'mute',
'playerready',
'playlist',
'playlistfile',
'playlistsize',
'plugins',
'provider',
'repeat',
'screencolor',
'shuffle',
'skin',
'smoothing',
'start',
'streamer',
'stretching',
'tags',
'title',
'volume'
];
// Return global function name that's not being used
function getFunctionName(base) {
var name, i = 0;
while (true) {
name = pluginName + '_' + base;
if (i) {
name += i;
}
if (!window[name]) {
return name;
}
i++;
}
}
$.fn[pluginName] = function (opts) {
var fn, args, data, prop;
if (typeof opts == 'string') { // it's a method name
fn = $.fn[pluginName][opts];
if (!fn) {
throw('No such method: ' + pluginName + '.' + opts);
}
args = $.makeArray(arguments).slice(1);
data = this.data(pluginName);
if (!data) {
throw('Constructor not called: ' + pluginName + '.' + opts);
}
args.unshift(data);
return fn.apply(this, args);
}
opts = $.extend({}, $.fn[pluginName].defaults, opts);
// put flashvars into subobject
for (prop in opts) {
if ($.inArray(prop, flashvarNames) != -1) {
opts.flashvars[prop] = opts[prop];
delete opts[prop];
}
}
// Convert integers to pixels
if (opts.height.toString().match(/^[0-9]+$/)) {
opts.height += 'px';
}
if (opts.width.toString().match(/^[0-9]+$/)) {
opts.width += 'px';
}
return this.each( function (index) {
var id = opts.id,
$this = $(this);
if (index) {
id += '-' + index;
}
if ($('#' + id).length) {
$.error('id "' + id + '" already exists in document');
}
function makeListener(propMap, listenerName) {
return function (obj) {
var n, seek;
for (n in propMap) {
$this[pluginName]('set', propMap[n], obj[n]);
}
if (listenerName == 'statelistener') {
seek = $this[pluginName]('get', 'seek');
if ((obj.newstate == 'PAUSED' ||
obj.newstate == 'PLAYING') && seek !== false) {
$this[pluginName]('set', 'seek', false);
$this[pluginName]('seek', seek);
}
}
if (opts[listenerName]) {
opts[listenerName](obj);
}
};
}
// Set up array of listeners to be added in playerReady.
var eventName, listenerName, propMap,
listeners = {};
for (eventName in eventType) {
listenerName = eventName.toLowerCase() + 'listener';
if (listenerProperties[eventName]) {
propMap = listenerProperties[eventName];
listeners[eventName] = makeListener(propMap, listenerName);
}
else if (opts[listenerName]) {
listeners[eventName] = opts[listenerName];
}
}
console.log('listeners', listeners);
// Create global function for playerReady that stores data,
// adds listeners, and then runs the playerready callback
// that was passed in the options, if there is one.
var fnName = getFunctionName('playerReady');
var extraPlayerready = opts.flashvars.playerready;
window[fnName] = function (obj) {
console.log(fnName + '()', arguments);
// Store player object (DOM node) in data() of jQuery object
var data = {
obj: $('#' + id)[0],
duration: null,
item: 0,
state: 'IDLE',
position: 0,
seek: false, // queued seek position
volume: null
};
$this.data(pluginName, data);
var eventName;
for (eventName in listeners) {
$this[pluginName]('addListener', eventName, listeners[eventName]);
}
$this.addClass(pluginName); // to signal it's finished
if (extraPlayerready) {
extraPlayerready(obj);
}
};
opts.flashvars.playerready = fnName;
$this.empty().flash( {
swf: opts.swf,
id: id,
name: id,
height: opts.height,
width: opts.width,
wmode: opts.wmode,
params: {
allowfullscreen: 'true',
allowscriptaccess: 'always',
enablejs: 'true',
flashvars: opts.flashvars
}
} );
return this;
} );
};
$.extend($.fn[pluginName], {
defaults: {
flashvars: {},
height: 240,
width: 320,
id: pluginName,
swf: 'player.swf',
wmode: 'opaque'
},
set: function (data, property, value) {
data[property] = value;
},
get: function (data, property) {
return data[property];
},
item: function (data, itemNumber) {
itemNumber = parseInt(itemNumber, 10);
console.log('jwPlayer.item', itemNumber);
data.obj.sendEvent('ITEM', itemNumber);
return this;
},
load: function(data, filename) {
console.log('jwPlayer.load: setting filename to ', filename);
data.obj.sendEvent('LOAD', filename);
return this;
},
mute: function (data) {
console.log('jwPlayer.mute');
data.obj.sendEvent('MUTE');
return this;
},
next: function (data) {
console.log('jwPlayer.next');
data.obj.sendEvent('NEXT');
return this;
},
pause: function (data) {
console.log('jwPlayer.pause');
data.obj.sendEvent('PLAY', 'false');
return this;
},
play: function (data) {
console.log('jwPlayer.play');
data.obj.sendEvent('PLAY', 'true');
return this;
},
resume: function (data) { // same as play
console.log('jwPlayer.resume');
data.obj.sendEvent('PLAY', 'true');
return this;
},
start: function (data) { // same as play
console.log('jwPlayer.start');
data.obj.sendEvent('PLAY', 'true');
return this;
},
togglePlay: function (data) {
console.log('jwPlayer.togglePlay');
data.obj.sendEvent('PLAY');
return this;
},
prev: function (data) {
console.log('jwPlayer.prev');
data.obj.sendEvent('PREV');
return this;
},
redraw: function (data) {
console.log('jwPlayer.redraw');
data.obj.sendEvent('REDRAW');
return this;
},
seek: function (data, position) {
var state;
console.log('jwPlayer.seek', position);
position = parseFloat(position);
if (!position) {
position = 0.001; // weird bug when seeking to 0
}
state = this[pluginName]('get', 'state');
console.log('state', state);
if (state == 'PLAYING' || state == 'PAUSED') {
data.obj.sendEvent('SEEK', position);
data.seek = false;
}
else {
if (state == 'IDLE') {
// Play and pause to get media loading
data.obj.sendEvent('PLAY', 'true');
data.obj.sendEvent('PLAY', 'false');
}
data.seek = position;
}
return this;
},
stop: function (data) {
console.log('jwPlayer.stop');
data.obj.sendEvent('STOP');
return this;
},
volume: function (data, volume) {
volume = parseInt(volume, 10);
data.obj.sendEvent('VOLUME', volume);
return this;
},
getConfig: function (data, property) {
var config = data.obj.getConfig();
return property ? config[property] : config;
},
getPlaylist: function (data, itemNumber) {
var playlist = data.obj.getPlaylist();
return itemNumber || itemNumber === 0 ? playlist[itemNumber] : playlist;
},
addListener: function (data, eventName, callback) {
console.log('jwPlayer.addListener', eventName, callback);
var fnName,
method = 'add' + eventType[eventName] + 'Listener';
if ($.isFunction(callback)) {
fnName = getFunctionName(eventName.toLowerCase() + 'Listener');
window[fnName] = callback;
callback = fnName;
}
return data.obj[method](eventName, callback);
},
destroy: function () {
console.log('jwPlayer.destroy');
$(this).removeData(pluginName).empty();
}
});
})(jQuery);