-
Notifications
You must be signed in to change notification settings - Fork 71
/
directive.js
483 lines (463 loc) · 17.2 KB
/
directive.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
/**
* MDN references for hackers:
* ===========================
* Media events on <audio> and <video> tags:
* https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events
* Properties and Methods:
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
* Internet Exploder version:
* http://msdn.microsoft.com/en-us/library/ff975061(v=vs.85).aspx
*
* Understanding TimeRanges objects:
* http://html5doctor.com/html5-audio-the-state-of-play/
*/
angular.module('mediaPlayer', ['mediaPlayer.helpers'])
.value('mp.throttleSettings', {
enabled: true,
time: 1000
})
.constant('mp.playerDefaults', {
// general properties
currentTrack: 0,
ended: undefined,
network: undefined,
playing: false,
seeking: false,
tracks: 0,
volume: 1,
// formatted properties
formatDuration: '00:00',
formatTime: '00:00',
loadPercent: 0
})
.directive('mediaPlayer', ['$rootScope', '$interpolate', '$timeout', 'mp.throttle', 'mp.playerDefaults', 'mp.throttleSettings',
function ($rootScope, $interpolate, $timeout, throttle, playerDefaults, tr) {
var playerMethods = {
/**
* @usage load([mediaElement], [autoplay]);
*
* @param {mediaElement Obj} mediaElement a single mediaElement, may contain multiple <source>(s)
* @param {boolean} autoplay: flag to autostart loaded element
*/
load: function (mediaElement, autoplay) {
// method overloading
if (typeof mediaElement === 'boolean') {
autoplay = mediaElement;
mediaElement = null;
} else if (typeof mediaElement === 'object') {
this.$clearSourceList();
this.$addSourceList(mediaElement);
}
this.$domEl.load();
this.ended = undefined;
if (autoplay) {
this.$element.one('canplay', this.play.bind(this));
}
},
/**
* resets the player (clear the <source>s) and reloads the playlist
*
* @usage reset([autoplay]);
* @param {boolean} autoplay: flag to autoplay once resetted
*/
reset: function (autoplay) {
angular.extend(this, playerDefaults);
this.$clearSourceList();
this.load(this.$playlist, autoplay);
},
/**
* @usage play([index], [selectivePlay])
* @param {integer} index: playlist index (0...n), to start playing from
* @param {boolean} selectivePlay: `true` will only play the specified or current track, 'false' will reset the flag to default
* The default is to continue playing the next one.
*/
play: function (index, selectivePlay) {
// method overloading
if (typeof index === 'boolean') {
selectivePlay = index;
index = undefined;
}
if (selectivePlay) {
this.$selective = true;
} else if (selectivePlay == false) {
this.$selective = false;
}
if (this.$playlist.length > index) {
this.currentTrack = index + 1;
return this.load(this.$playlist[index], true);
}
// readyState = HAVE_NOTHING (0) means there's nothing into the <audio>/<video> tag
if (!this.currentTrack && this.$domEl.readyState) { this.currentTrack++; }
// In case the stream is completed, reboot it with a load()
if (this.ended) {
this.load(true);
} else {
this.$domEl.play();
}
},
playPause: function (index, selectivePlay) {
// method overloading
if (typeof index === 'boolean') {
selectivePlay = index;
index = undefined;
}
if (selectivePlay) {
this.$selective = true;
} else if (selectivePlay == false) {
this.$selective = false;
}
if (typeof index === 'number' && index + 1 !== this.currentTrack) {
this.play(index);
} else if (this.playing) {
this.pause();
} else {
this.play();
}
},
pause: function () {
this.$domEl.pause();
},
stop: function () {
this.reset();
},
toggleMute: function () {
this.muted = this.$domEl.muted = !this.$domEl.muted;
},
next: function (autoplay) {
var self = this;
if (self.currentTrack && self.currentTrack < self.tracks) {
var wasPlaying = autoplay || self.playing;
self.pause();
$timeout(function () {
self.$clearSourceList();
self.$addSourceList(self.$playlist[self.currentTrack]);
self.load(wasPlaying); // setup autoplay here.
self.currentTrack++;
});
}
},
prev: function (autoplay) {
var self = this;
if (self.currentTrack && self.currentTrack - 1) {
var wasPlaying = autoplay || self.playing;
self.pause();
$timeout(function () {
self.$clearSourceList();
self.$addSourceList(self.$playlist[self.currentTrack - 2]);
self.load(wasPlaying); // setup autoplay here.
self.currentTrack--;
});
}
},
setPlaybackRate: function (value) {
this.$domEl.playbackRate = value;
},
setVolume: function (value) {
this.$domEl.volume = value;
},
seek: function (value) {
var doubleval = 0, valuesArr;
if (typeof value === 'string') {
valuesArr = value.split(':');
doubleval += parseInt(valuesArr.pop(), 10);
if (valuesArr.length) { doubleval += parseInt(valuesArr.pop(), 10) * 60; }
if (valuesArr.length) { doubleval += parseInt(valuesArr.pop(), 10) * 3600; }
if (!isNaN(doubleval)) {
return this.$domEl.currentTime = doubleval;
}
} else {
return this.$domEl.currentTime = value;
}
},
/**
* binds a specific event directly to the element
* @param {string} type event name
* @param {function} fn callback
* @return {function} unbind function, call it in order to remove the listener
*/
on: function (type, fn) {
return this.$element.on(type, fn);
},
off: function (type, fn) {
return this.$element.off(type, fn);
},
one: function (type, fn) {
return this.$element.one(type, fn);
},
$addSourceList: function (sourceList) {
var self = this;
if (angular.isArray(sourceList)) {
angular.forEach(sourceList, function (singleElement, index) {
var sourceElem = document.createElement('SOURCE');
['src', 'type', 'media'].forEach(function (key) { // use only a subset of the properties
if (singleElement[key] !== undefined) { // firefox is picky if you set undefined attributes
sourceElem.setAttribute(key, singleElement[key]);
}
});
self.$element.append(sourceElem);
});
} else if (angular.isObject(sourceList)) {
var sourceElem = document.createElement('SOURCE');
['src', 'type', 'media'].forEach(function (key) {
if (sourceList[key] !== undefined) {
sourceElem.setAttribute(key, sourceList[key]);
}
});
self.$element.append(sourceElem);
}
},
$clearSourceList: function () {
this.$element.contents().remove();
},
$formatTime: function (seconds) {
if (seconds === Infinity) {
return '∞'; // If the data is streaming
}
var hours = parseInt(seconds / 3600, 10) % 24,
minutes = parseInt(seconds / 60, 10) % 60,
secs = parseInt(seconds % 60, 10),
result,
fragment = (minutes < 10 ? '0' + minutes : minutes) + ':' + (secs < 10 ? '0' + secs : secs);
if (hours > 0) {
result = (hours < 10 ? '0' + hours : hours) + ':' + fragment;
} else {
result = fragment;
}
return result;
},
$attachPlaylist: function (pl) {
if (pl === undefined || pl === null) {
this.playlist = [];
} else {
this.$playlist = pl;
}
}
};
/**
* Binding function that gives life to AngularJS scope
* @param {Scope} au Player Scope
* @param {HTMLMediaElement} HTML5 element
* @param {jQlite} element <audio>/<video> element
* @return {function}
*
* Returns an unbinding function
*/
var bindListeners = function (au, al, element) {
var listeners = {
playing: function () {
au.$apply(function (scope) {
scope.playing = true;
scope.ended = false;
});
},
pause: function () {
au.$apply(function (scope) {
scope.playing = false;
});
},
ended: function () {
if (!au.$selective && au.currentTrack < au.tracks) {
au.next(true);
} else {
au.$apply(function (scope) {
scope.ended = true;
scope.playing = false; // IE9 does not throw 'pause' when file ends
});
}
},
timeupdate: function () {
au.$apply(function (scope) {
scope.currentTime = al.currentTime;
scope.formatTime = scope.$formatTime(scope.currentTime);
});
},
loadedmetadata: function () {
au.$apply(function (scope) {
if (!scope.currentTrack) { scope.currentTrack++; } // This is triggered *ONLY* the first time a <source> gets loaded.
scope.duration = al.duration;
scope.formatDuration = scope.$formatTime(scope.duration);
if (al.buffered.length) {
scope.loadPercent = Math.round((al.buffered.end(al.buffered.length - 1) / scope.duration) * 100);
}
});
},
progress: function () {
if (au.$domEl.buffered.length) {
au.$apply(function (scope) {
scope.loadPercent = Math.round((al.buffered.end(al.buffered.length - 1) / scope.duration) * 100);
scope.network = 'progress';
});
}
},
volumechange: function () { // Sent when volume changes (both when the volume is set and when the muted attribute is changed).
au.$apply(function (scope) {
// scope.volume = Math.floor(al.volume * 100);
scope.volume = al.volume;
scope.muted = al.muted;
});
},
seeked: function () {
au.$apply(function (scope) {
scope.seeking = false;
});
},
seeking: function () {
au.$apply(function (scope) {
scope.seeking = true;
});
},
ratechange: function () {
au.$apply(function (scope) {
// scope.playbackRate = Math.floor(al.playbackRate * 100);
scope.playbackRate = al.playbackRate;
});
},
stalled: function () {
au.$apply(function (scope) {
scope.network = 'stalled';
});
},
suspend: function () {
au.$apply(function (scope) {
scope.network = 'suspend';
});
}
};
if (tr.enabled) {
listeners.timeupdate = throttle(tr.time, false, listeners.timeupdate);
}
angular.forEach(listeners, function (f, listener) {
element.on(listener, f);
});
};
var MediaPlayer = function (element) {
var mediaScope = angular.extend($rootScope.$new(true), {
$element: element,
$domEl: element[0],
$playlist: undefined,
// bind TimeRanges structures to actual MediaElement
buffered: element[0].buffered,
played: element[0].played,
seekable: element[0].seekable,
}, playerDefaults, playerMethods);
bindListeners(mediaScope, element[0], element);
return mediaScope;
};
// creates a watch function bound to the specific player
// optimizable: closures eats ram
function playlistWatch(player) {
return function (playlistNew, playlistOld, watchScope) {
var currentTrack,
newTrackNum = null;
// on every playlist change, it refreshes the reference, safer/shorter approach
// than using multiple ifs and refresh only if it changed; there's no benefit in that
player.$attachPlaylist(playlistNew);
if (playlistNew === undefined && playlistOld !== undefined) {
return player.pause();
}
/**
* Playlist update logic:
* If the player has started ->
* Check if the playing track is in the new Playlist [EXAMPLE BELOW]
* If it is ->
* Assign to it the new tracknumber
* Else ->
* If the new Playlist has some song ->
* Pause the player, and get the new Playlist
* Else ->
* Reset the player, and await for orders
*
* Else (if the player hasn't started yet)
* Just replace the <src> tags
*
* Example
* playlist: [a,b,c], playing: c, trackNum: 2
* ----delay 5 sec-----
* playlist: [f,a,b,c], playing: c, trackNum: 3
*
*/
if (player.currentTrack) {
currentTrack = playlistOld ? playlistOld[player.currentTrack - 1] : -1;
for (var i = 0; i < playlistNew.length; i++) {
if (angular.equals(playlistNew[i], currentTrack)) { newTrackNum = i; break; }
}
if (newTrackNum !== null) { // currentTrack it's still in the new playlist, update trackNumber
player.currentTrack = newTrackNum + 1;
player.tracks = playlistNew.length;
} else { // currentTrack has been removed.
player.pause();
if (playlistNew.length) { // if the new playlist has some elements, replace actual.
$timeout(function () { // need $timeout because the mediaTag needs a little time to launch the 'pause' event
player.$clearSourceList();
player.$addSourceList(playlistNew[0]);
player.load();
player.tracks = playlistNew.length;
});
} else { // the new playlist has no elements, clear actual
player.reset();
}
}
} else if (playlistNew.length) { // new playlist has elements, load them
player.$clearSourceList();
player.$addSourceList(playlistNew[0]);
player.load();
player.tracks = playlistNew.length;
} else { // new playlist has no elements, clear actual
player.reset();
}
};
}
return {
/**
* The directive uses the Scope in which gets declared,
* so it can read/write it with ease.
* The only isolated Scope here gets created for the MediaPlayer.
*/
scope: false,
link: function (scope, element, attrs, ctrl) {
var playlistName = attrs.playlist,
mediaName = attrs.mediaPlayer || attrs.playerControl;
var player = new MediaPlayer(element), playlist = scope[playlistName];
// create data-structures in the father Scope
if (playlistName === undefined) {
playlist = []; // local playlist gets defined as new
} else if (scope[playlistName] === undefined) {
playlist = scope[playlistName] = []; // define playlist on father scope
} else {
playlist = scope[playlistName];
}
if (mediaName !== undefined) { scope[mediaName] = player; }
if (element[0].tagName !== 'AUDIO' && element[0].tagName !== 'VIDEO') {
return new Error('player directive works only when attached to an <audio>/<video> type tag');
}
var mediaElement = [],
sourceElements = element.find('source');
// create a single playlist element from <source> tag(s).
// if the <source> tag is one, use object notation...
if (sourceElements.length === 1) {
playlist.unshift({ src: sourceElements[0].src, type: sourceElements[0].type, media: sourceElements[0].media });
} else if (sourceElements.length > 1) { // otherwise use array notation
angular.forEach(sourceElements, function (sourceElement) {
mediaElement.push({ src: sourceElement.src, type: sourceElement.type, media: sourceElement.media });
});
// put mediaElement as first element in the playlist
playlist.unshift(mediaElement);
}
/**
* If the user wants to keep track of the playlist changes
* has to use data-playlist="variableName" in the <audio>/<video> tag
*
* Otherwise: it will be created an empty playlist and attached to the player.
*/
if (playlistName === undefined) {
player.$attachPlaylist(playlist); // empty playlist case
} else if (playlist.length) {
playlistWatch(player)(playlist, undefined, scope); // playlist already populated gets bootstrapped
scope.$watch(playlistName, playlistWatch(player), true); // then watch gets applied
} else {
scope.$watch(playlistName, playlistWatch(player), true); // playlist empty, only watch
}
}
};
}]
);