-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_multimedia.js
515 lines (415 loc) · 16.9 KB
/
_multimedia.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
_
(_)
_ _____
| | / ____|
| | | (___
_ | | \___ \
______ _ | |__| | ____) |
|______| (_) \____/ |______/
v0.0.7
https://www.github.com/wesdegroot/_.js/
or https://www.wdgwv.com
Git: https://github.com/wesdegroot/_.js
Todo: https://github.com/wesdegroot/_.js/issues
Licence: https://github.com/wesdegroot/_.js/blob/master/LICENCE.md (CC BY 4.0)
Latest: https://raw.githubusercontent.com/wesdegroot/_.js/master/latest/_.js
*/
if (typeof _ !== 'function') {
window.alert('Please make sure _.js is loaded!')
} else {
window.players = {} // the media players
window.video = 'video' // video -> video (if user forgets '')
window.audio = 'audio' // audio -> audio (if user forgets '')
window.MMActive = '' // Wich player is active?
window.MMTime = 0 // What is the playing time?
window.MMLTime = 0 // What is the (last) playing time?
window.MMisSeek = false // Is seeking?
/**
* multimedia
*
* Add a nice multimedia parser
*
* @param object object
* @param data the configuration array.
* @see https://github.com/wesdegroot/_.js/wiki/module_multimedia
* @example _().multimedia()
*/
window._.fn.multimedia = function (data) {
var len = this.length
while (len--) {
var mediaType = {
mp3: 'audio',
ogg: 'ERROR',
wav: 'audio',
mp4: 'video',
webm: 'video'
}
var audioTypes = {
mp3: 'audio/mpeg',
ogg: 'audio/ogg',
wav: 'audio/wav',
webm: 'audio/webm'
}
var videoTypes = {
mp4: 'video/mp4',
ogg: 'video/ogg',
webm: 'video/webm'
}
var options = {
autoplay: true,
controls: false,
visible: false, // Deprecated. (yes that's soon, it's useless (use's parent class ;))
default: true,
type: 'ERROR'
}
// is "data" a object (array thing)
if (typeof data === 'object') {
// if file is undefined, break.
if (this.isUndefined(data['file'])) {
console.error('Need a file!')
console.error('Please see: https://github.com/wesdegroot/_.js/wiki/module_multimedia')
break
} else {
// if object has options
if (typeof data['options'] !== 'undefined') {
// and a type!
if (typeof data['options']['type'] !== 'undefined') {
options['type'] = data['options']['type']
} else {
if (typeof data['file'] === 'object') {
options['type'] = mediaType[data['file'][0].substr(data['file'][0].indexOf('.') + 1)]
} else {
options['type'] = mediaType[data['file'].substr(data['file'].indexOf('.') + 1)]
}
}
// autoplay on? off?
if (typeof data['options']['autoplay'] !== 'undefined') options['autoplay'] = data['options']['autoplay']
// controls visible?
if (typeof data['options']['controls'] !== 'undefined') options['controls'] = data['options']['controls']
// must the player be visible?
if (typeof data['options']['visible'] !== 'undefined') options['visible'] = data['options']['visible']
// Not the default config anymore!
options['default'] = false
} else {
// no options given, so default ones.
options['type'] = mediaType[data['file'].substr(data['file'].indexOf('.') + 1)]
}
// shit, default, but it's an ogg (audio or video...)
if (options['type'] === 'ERROR') {
console.error("File extension is .ogg!, we can't detect if it is a audio or a video file. sorry.")
console.error('Please see: https://github.com/wesdegroot/_.js/wiki/module_multimedia')
}
}
// Clean out the placeholder
this[len].innerHTML = ''
// Play ad (before)
if (!this.isUndefined(data['before'])) {
// has his own data :D
if (typeof data['before'] === 'object') {
// Parse things
console.error('Not yet supported :(')
console.error('Please see: https://github.com/wesdegroot/_.js/wiki/module_multimedia')
} else {
// even better. our default data.
// Create "MMElement" (MultiMediaElement)
var MMElement = document.createElement(mediaType[data['before'].substr(data['before'].indexOf('.') + 1)])
// Does it need controls? [AD NEED NEVER CONTOLS]
// if (options['controls'])
// MMElement.setAttribute("controls", "yes")
// Pre load ;)
MMElement.setAttribute('preload', 'auto')
// Set Height (100%)
MMElement.setAttribute('height', '100%')
// Set width
MMElement.setAttribute('width', '100%')
// And hack it ;)
MMElement.setAttribute('style', 'width:100%;height:100%;')
// Set the id
MMElement.setAttribute('id', 'MMMain0')
// Autobuffer ;) (we dont like delay)
MMElement.autobuffer = true
// Create the source
var source = document.createElement('source')
// What is the type of the file?
if (mediaType[data['before'].substr(data['before'].indexOf('.') + 1)] !== 'video') {
source.type = audioTypes[data['before'].substr(data['before'].indexOf('.') + 1)]
} else {
source.type = videoTypes[data['before'].substr(data['before'].indexOf('.') + 1)]
}
// And the SouRCe
source.src = data['before']
// Append source to "MMElement"
MMElement.appendChild(source)
// Start loading
MMElement.load()
// Do we have autoplay?
if (options['autoplay']) MMElement.play()
window.MMActive = 0
// Kill seeking (skipping ad)
MMElement.addEventListener('seeking', function (event) {
// Warn the 'timeupdate' that the user is seeking, don't save anything!
window.MMisSeek = true
// if seeked more then now, then revert
if (this.currentTime > window.MMLTime) {
this.currentTime = window.MMLTime
}
// + unset seeking action (always)
window.MMisSeek = false
})
MMElement.addEventListener('timeupdate', function (event) {
// save the time
if (!window.MMisSeek) {
// save the 'last' time (seeking sets time)
window.MMLTime = window.MMTime
// set the time
window.MMTime = this.currentTime
}
})
// On end play the next.
MMElement.addEventListener('ended', function () {
// Pause this one
this.pause()
// make me hidden
this.style.visibility = 'hidden'
this.style.display = 'none'
// make the second visible
document.getElementById('MMMain1').style.visibility = ''
document.getElementById('MMMain1').style.display = 'block'
// Play the next
document.getElementById('MMMain1').play()
// Wich player is active
window.MMActive = 1
// Reset player time
window.MMTime = 0
})
// Add to HTML
this[len].appendChild(MMElement)
// Add Player to the global window.players list (for skip, pause, active, multiple files)
window.players = this.merge(window.players, {MMMain0: {file: data['before'], html: MMElement, options: options}})
}
}
if (typeof data['file'] !== 'object') {
data['file'] = [data['file']]
}
for (var i = 0; i < data['file'].length; i++) {
var now = this.isUndefined(data['before']) ? i : i + 1
// Main file
// Create "MMElement" (MultiMediaElement)
MMElement = document.createElement(mediaType[data['file'][i].substr(data['file'][i].indexOf('.') + 1)])
// Does it need controls?
if (options['controls']) MMElement.setAttribute('controls', 'yes')
// Pre load ;)
MMElement.setAttribute('preload', 'auto')
// Set Height (100%)
MMElement.setAttribute('height', '100%')
// Set width
MMElement.setAttribute('width', '100%')
// And hack it ;)
if (this.isUndefined(data['before'])) {
MMElement.setAttribute('style', 'width:100%;height:100%;')
} else {
MMElement.setAttribute('style', 'visibility:hidden;display:none;width:100%;height:100%;')
}
// Set the id
MMElement.setAttribute('id', 'MMMain' + now)
// Wich player is active
if (this.isUndefined(data['before'])) window.MMActive = MMElement
// No ad, but autoplay ;)
if (options['autoplay'] && this.isUndefined(data['before'])) MMElement.play()
// Autobuffer ;) (we dont like delay)
MMElement.autobuffer = true
// Create the source
source = document.createElement('source')
// What is the type of the file?
if (options['type'] !== 'video') {
source.type = audioTypes[data['file'][i].substr(data['file'][i].indexOf('.') + 1)]
} else {
source.type = videoTypes[data['file'][i].substr(data['file'][i].indexOf('.') + 1)]
}
// And the SouRCe
source.src = data['file'][i]
// Append source to "MMElement"
MMElement.appendChild(source)
// Start loading
MMElement.load()
MMElement.addEventListener('ended', function () {
// Wich one is playing now?
now = window.MMActive
// Does the second exists?
if (document.getElementById('MMMain' + (now + 1)) != null) {
// Pause this one
this.pause()
// make me hidden
this.style.visibility = 'hidden'
this.style.display = 'none'
// make the second visible
document.getElementById('MMMain' + (now + 1)).style.visibility = ''
document.getElementById('MMMain' + (now + 1)).style.display = 'block'
// Play the next
document.getElementById('MMMain' + (now + 1)).play()
// Wich player is active
window.MMActive = now + 1
// Reset playing time
window.MMTime = 0
}
})
// Add to HTML
this[len].appendChild(MMElement)
// Add Player to the global window.players list (for skip, pause, active, multiple files)
/*eslint-disable */
eval('window.players=this.merge(window.players,{MMMain' + now + ":{file:data['file'][i],html:MMElement,options:options}});")
/* eslint-enable */
}
// Ad on end.
if (!this.isUndefined(data['after'])) {
if (typeof data['after'] === 'object') {
// Parse things
console.error('Not yet supported :(')
console.error('Please see: https://github.com/wesdegroot/_.js/wiki/module_multimedia')
} else {
// Create "MMElement" (MultiMediaElement)
MMElement = document.createElement(mediaType[data['after'].substr(data['after'].indexOf('.') + 1)])
// Does it need controls? [AD NEED NEVER CONTOLS]
// if (options['controls'])
// MMElement.setAttribute("controls", "yes")
// Pre load ;)
MMElement.setAttribute('preload', 'auto')
// Set Height (100%)
MMElement.setAttribute('height', '100%')
// Set width
MMElement.setAttribute('width', '100%')
// And hack it ;)
MMElement.setAttribute('style', 'visibility:hidden;display:none;width:100%;height:100%;')
// Set the id
MMElement.setAttribute('id', 'MMMain' + (now + 1))
// Autobuffer ;) (we dont like delay)
MMElement.autobuffer = true
// Create the source
source = document.createElement('source')
// What is the type of the file?
if (mediaType[data['after'].substr(data['after'].indexOf('.') + 1)] !== 'video') {
source.type = audioTypes[data['after'].substr(data['after'].indexOf('.') + 1)]
} else {
source.type = videoTypes[data['after'].substr(data['after'].indexOf('.') + 1)]
}
// What is the type of the file?
source.src = data['after']
// Append source to "MMElement"
MMElement.appendChild(source)
// Start loading
MMElement.load()
// Kill seeking (skipping ad)
MMElement.addEventListener('seeking', function (event) {
// Warn the 'timeupdate' that the user is seeking, don't save anything!
window.MMisSeek = true
// if seeked more then now, then revert
if (this.currentTime > window.MMLTime) {
this.currentTime = window.MMLTime
}
// + unset seeking action (always)
window.MMisSeek = false
})
MMElement.addEventListener('timeupdate', function (event) {
// save the time
if (!window.MMisSeek) {
// save the 'last' time (seeking sets time)
window.MMLTime = window.MMTime
// set the time
window.MMTime = this.currentTime
}
})
// On end pause
MMElement.addEventListener('ended', function () {
// Pause, all done my friend.
this.pause()
// And reset playing time
window.MMTime = 0
})
// Add to HTML
this[len].appendChild(MMElement)
// Add Player to the global window.players list (for skip, pause, active, multiple files)
/*eslint-disable */
eval('window.players=this.merge(window.players,{MMMain' + (now + 1) + ":{file:data['after'],html:MMElement,options:options}});")
/* eslint-enable */
}
}
} else {
console.error('Please see: https://github.com/wesdegroot/_.js/wiki/module_multimedia')
}
}
}
/**
* multimedia_start
*
* multimedia player, start
*
* @param object object
* @see https://github.com/wesdegroot/_.js/wiki/module_multimedia
* @example _().multimedia_start()
*/
window._.fn.multimedia_start = function () {
// Start current player
document.getElementById('MMMain' + window.MMActive).play()
}
/**
* multimedia_pause
*
* multimedia player, pause
*
* @param object object
* @see https://github.com/wesdegroot/_.js/wiki/module_multimedia
* @example _().multimedia_pause()
*/
window._.fn.multimedia_pause = function () {
// Start current player
document.getElementById('MMMain' + window.MMActive).pause()
}
/**
* multimedia_stop
*
* multimedia player, stop
*
* @param object object
* @see https://github.com/wesdegroot/_.js/wiki/module_multimedia
* @example _().multimedia_stop()
*/
window._.fn.multimedia_stop = function () {
// Start current player
document.getElementById('MMMain' + window.MMActive).pause()
// Reset playing time
window.MMTime = 0
// And reset time on element
document.getElementById('MMMain' + window.MMActive).currentTime = 0
}
/**
* multimedia_skip
*
* multimedia player, skip
*
* @param object object
* @see https://github.com/wesdegroot/_.js/wiki/module_multimedia
* @example _().multimedia_skip()
*/
window._.fn.multimedia_skip = function () {
// Is there one more?
if (document.getElementById('MMMain' + (window.MMActive + 1)) != null) {
// Pause this one
document.getElementById('MMMain' + window.MMActive).pause()
// make me hidden
document.getElementById('MMMain' + window.MMActive).style.visibility = 'hidden'
document.getElementById('MMMain' + window.MMActive).style.display = 'none'
// make the second visible
document.getElementById('MMMain' + (window.MMActive + 1)).style.visibility = ''
document.getElementById('MMMain' + (window.MMActive + 1)).style.display = 'block'
// Play the next
document.getElementById('MMMain' + (window.MMActive + 1)).play()
// Wich player is active
window.MMActive = window.MMActive + 1
// Reset playing time
window.MMTime = 0
} else {
console.error('Please see: https://github.com/wesdegroot/_.js/wiki/module_multimedia')
}
}
}