-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathPlayer.js
521 lines (448 loc) · 12.8 KB
/
Player.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
516
517
518
519
520
521
import Electron from 'electron';
import Notifier from './Notifier';
import { EventEmitter } from 'events';
import videojs from 'video.js';
videojs.options.flash.swf = 'dist/vendor/video.js/dist/video-js.swf';
const Remote = Electron.remote;
const Dialog = Remote.dialog;
const App = Remote.app;
import PreferenceManager from '../../modules/PreferenceManager';
import TrackInfoFetcher from 'kaku-core/modules/TrackInfoFetcher';
import HistoryManager from '../../modules/HistoryManager';
import L10nManager from '../../modules/L10nManager';
import AppCore from '../../modules/AppCore';
import Searcher from '../../modules/Searcher';
import Tracker from '../../modules/Tracker';
import Defer from 'kaku-core/modules/Defer';
import os from 'os';
TrackInfoFetcher.setPath(App.getPath('userData'))
const _ = L10nManager.get.bind(L10nManager);
// This should be a wrapper of Videojs and expose
// needed events / API out for callee to use
function Player() {
EventEmitter.call(this);
this.playingTrack = null;
this.playingTrackTime = 0;
this.trackIndex = -1;
this.randomIndex = -1;
this.randomIndexes = [];
this.tracks = [];
this.disabled = false;
this.modes = ['no', 'one', 'all', 'random'];
this.mode = this.modes[0];
this._playerDOM = null;
this._player = null;
this._pendingReadyDefers = [];
}
Player.prototype = Object.create(EventEmitter.prototype);
Player.constructor = Player;
Player.cache = {};
// Because we share on() for customized & vjs's event,
// we need to bind to the right place when using it.
Player.CUSTOMIZED_EVENTS = [
'modeUpdated',
'tracksUpdated'
];
Player.prototype.changeMode = function(mode) {
// like Player.changeMode('no')
if (typeof mode === 'string') {
let index = this.modes.indexOf(mode);
if (index >= 0) {
this.mode = mode;
this.emit('modeUpdated', this.mode);
}
}
// like Player.changeMode() - jump to next one
else {
let maxLength = this.modes.length;
let index = this.modes.indexOf(this.mode);
index = (index + 1) % maxLength;
let newIndex = Math.max(0, Math.min(index, maxLength - 1));
this.mode = this.modes[newIndex];
this.emit('modeUpdated', this.mode);
}
};
Player.prototype._getDefaultVideoJSConfig = function() {
return {
width: '100%',
height: 'auto',
autoplay: false,
bigPlayButton: false,
controls: true,
controlBar: {
playToggle: false,
fullscreenToggle: true,
muteToggle: false
}
};
};
Player.prototype._addPlayerEvents = function() {
this._player.on('timeupdate', () => {
let time = this._player.currentTime();
this.playingTrackTime = time || 0;
});
this._player.on('ended', () => {
this.playNextTrack();
});
this._player.on('volumechange', () => {
PreferenceManager.setPreference('default.volume', this._player.volume());
});
this._player.on('error', (error) => {
Notifier.alert('Unknown errors, please try again');
// Some hints
console.log('Please report following errors to us, thanks !');
console.log(error);
});
};
Player.prototype._toggleSpinner = function(show) {
// Because _player.loadingSpinner.show() doesn't work in this case,
// we have to forcely toggle vjs-waiting class to make the loading
// spinner work.
let elem = this._player.el();
if (show) {
elem.classList.add('vjs-waiting');
}
else {
elem.classList.remove('vjs-waiting');
}
};
Player.prototype.exitFullscreen = function() {
this.ready().then(() => {
if (this._player.isFullscreen()) {
this._player.exitFullscreen();
}
});
};
Player.prototype.setPlayer = function(playerDOM) {
this._playerDOM = playerDOM;
};
Player.prototype.ready = function() {
if (!this._playerDOM) {
let defer = Defer();
this._pendingReadyDefers.push(defer);
return defer.promise;
}
else if (this._player) {
return Promise.resolve(this._player);
}
else {
let self = this;
let promise = new Promise((resolve) => {
videojs(
this._playerDOM,
this._getDefaultVideoJSConfig()
).ready(function() {
self._player = this;
self._addPlayerEvents();
self._executePendingReadyDefers();
// return real videojs-ed player out
resolve(self._player);
});
});
return promise;
}
};
Player.prototype.addTracks = function(tracks, noUpdate) {
if (this.disabled) {
return;
}
this.tracks = [].concat(this.tracks, tracks);
this.randomIndexes = this.makeRandomIndexes(this.tracks.length);
if (!noUpdate) {
this.emit('tracksUpdated', this.tracks);
}
};
Player.prototype.cleanupTracks = function(noUpdate) {
if (this.disabled) {
return;
}
this.tracks = [];
if (!noUpdate) {
this.emit('tracksUpdated', this.tracks);
}
};
Player.prototype.playNextTrack = function(forceIndex) {
if (this.disabled) {
return;
}
if (typeof forceIndex !== 'undefined') {
this.trackIndex = Math.max(0, Math.min(forceIndex, this.tracks.length -1));
// TODO
// double check this randomIndex later
this.randomIndex = this.trackIndex;
this.play(this.tracks[this.trackIndex]);
return;
}
if (this.mode === 'no') {
this.trackIndex += 1;
if (this.trackIndex >= this.tracks.length) {
this.stop();
}
else {
this.trackIndex = Math.min(this.trackIndex, this.tracks.length - 1);
this.play(this.tracks[this.trackIndex]);
}
}
else if (this.mode === 'random') {
this.randomIndex += 1;
if (this.randomIndex >= this.tracks.length) {
this.stop();
}
else {
this.randomIndex = Math.min(this.randomIndex, this.tracks.length - 1);
this.trackIndex = this.randomIndexes[this.randomIndex];
this.play(this.tracks[this.trackIndex]);
}
}
else if (this.mode === 'one') {
this.trackIndex = Math.max(0, this.trackIndex);
this.play(this.tracks[this.trackIndex]);
}
else if (this.mode === 'all') {
this.trackIndex = (this.trackIndex + 1) % this.tracks.length;
this.play(this.tracks[this.trackIndex]);
}
};
Player.prototype.playPreviousTrack = function() {
if (this.disabled) {
return;
}
if (this.mode === 'no' || this.mode === 'random') {
this.trackIndex -= 1;
if (this.trackIndex < 0) {
this.stop();
}
else {
this.trackIndex = Math.max(this.trackIndex, 0);
this.play(this.tracks[this.trackIndex]);
}
}
else if (this.mode === 'random') {
this.randomIndex -= 1;
if (this.randomIndex < 0) {
this.stop();
}
else {
this.randomIndex = Math.max(this.randomIndex, 0);
this.trackIndex = this.randomIndexes[this.randomIndex];
this.play(this.tracks[this.trackIndex]);
}
}
else if (this.mode === 'one') {
this.trackIndex = Math.max(this.trackIndex, 0);
this.play(this.tracks[this.trackIndex]);
}
else if (this.mode === 'all') {
this.trackIndex = Math.max(0, this.trackIndex - 1);
this.play(this.tracks[this.trackIndex]);
}
};
Player.prototype.on = function() {
let args = arguments;
let eventName = args[0];
if (Player.CUSTOMIZED_EVENTS.indexOf(eventName) !== -1) {
this.constructor.prototype.on.apply(this, args);
}
else {
this.ready().then(() => {
this._player.on.apply(this._player, args);
});
}
};
Player.prototype.off = function() {
let args = arguments;
let eventName = args[0];
if (Player.CUSTOMIZED_EVENTS.indexOf(eventName) !== -1) {
this.constructor.prototype.removeEventListener.apply(this, args);
}
else {
this.ready().then(() => {
this._player.off.apply(this._player, args);
});
}
};
Player.prototype._prepareTrackData = function(rawTrack) {
let id = rawTrack.platformId;
if (Player.cache[id]) {
return Promise.resolve(Player.cache[id]);
}
else if (id && !Player.cache[id]) {
return Promise.resolve(rawTrack);
}
else {
// This is for clicking on tracks in Top Ranking
let promise = new Promise((resolve) => {
let keyword = rawTrack.artist + ' ' + rawTrack.title;
Searcher.search(keyword, 1).then(function(tracks) {
let trackInfo;
// NOTE
// We may not find any track when searching.
if (tracks.length > 0) {
trackInfo = tracks[0];
// NOTE
// we have to keep its original title, artist and covers
// in order not to confuse users
trackInfo.artist = rawTrack.artist;
trackInfo.title = rawTrack.title;
trackInfo.covers = rawTrack.covers;
}
resolve(trackInfo);
});
});
return promise;
}
};
Player.prototype._getRealTrack = function(track) {
let id = track.platformId;
if (Player.cache[id]) {
return Promise.resolve(Player.cache[id]);
}
else {
let trackUrl = track.platformTrackUrl;
return TrackInfoFetcher.getInfo(trackUrl).then((fetchedInfo) => {
track.platformTrackRealUrl= fetchedInfo.url;
track.ext = fetchedInfo.ext;
Player.cache[id] = track;
return Promise.resolve(track);
});
}
};
Player.prototype._executePendingReadyDefers = function() {
this._pendingReadyDefers.forEach((defer) => {
defer.resolve(this._player);
});
};
Player.prototype.setVolume = function(operation) {
// Videojs will check volume's max / min by itself,
// so we don't have to do extra check
this.ready().then(() => {
let currentVolume = this._player.volume();
switch (operation) {
case 'default':
this._player.volume(0.5);
break;
case 'mute':
this._player.volume(0);
break;
case 'up':
this._player.volume(currentVolume + 0.1);
break;
case 'down':
this._player.volume(currentVolume - 0.1);
break;
default:
let volume = parseFloat(operation, 10);
if (!isNaN(volume)) {
this._player.volume(volume);
}
break;
}
Tracker.event('Player', 'set volume', this._player.volume()).send();
});
};
Player.prototype.stop = function(force) {
if (this.disabled && !force) {
return;
}
this.ready().then(() => {
this.trackIndex = -1;
this.randomIndex = -1;
this._player.pause();
this._player.currentTime(0);
});
};
Player.prototype.playOrPause = function(force) {
if (this.disabled && !force) {
return;
}
this.ready().then(() => {
if (this._player.paused()) {
this.play();
}
else {
this.pause();
}
});
};
Player.prototype.pause = function(force) {
if (this.disabled && !force) {
return;
}
this.ready().then(() => {
this._player.pause();
});
};
Player.prototype.play = function(rawTrack, time, force) {
// This may be coming from Online DJ
let currentTime = time || 0;
// If the role is guest and the "play" request is not coming from DJ,
// then it means the user is trying to control the player by himself and
// this should be forbidden.
if (this.disabled && !force) {
return;
}
this.ready().then(() => {
// If we already have playingTrack in internal variable,
// when this.play() is called, we will directly play the same track
// instead of fetching from remote again.
if (!rawTrack) {
if (this._player.paused() && this.playingTrack) {
this._player.play();
}
}
else {
this._player.pause();
this._toggleSpinner(true);
this._prepareTrackData(rawTrack).then((foundTrack) => {
// we did find a track from searcher
if (foundTrack) {
this._getRealTrack(foundTrack).then((realTrack) => {
Tracker.event('Player', 'play',
realTrack.platformTrackUrl).send();
this.playingTrack = realTrack;
this._player.src(realTrack.platformTrackRealUrl);
this._player.currentTime(currentTime);
this._player.play();
this._toggleSpinner(false);
// keep this track into history
HistoryManager.add(realTrack);
// show notification on desktop if possible
Notifier.sendDesktopNotification({
title: realTrack.title,
body: realTrack.artist
});
});
}
else {
// TODO
// need l10n here
Notifier.alert('Sorry, we can\'t find the track in current ' +
'searcher, please try again with another searcher !');
this._toggleSpinner(false);
}
});
}
});
};
Player.prototype.makeRandomIndexes = function(length) {
// Fisher-Yates (aka Knuth) Shuffle
let temp;
let randomIndex;
let currentIndex = length;
// length = 5 -> array = [0, 1, 2, 3, 4]
let array = Array.from({
length: length
}, (v, k) => k);
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temp = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temp;
}
return array;
};
Player.prototype.disable = function(val) {
this.disabled = !!val;
};
module.exports = new Player();