-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
123 lines (108 loc) · 2.51 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
import EventHandler from './event_handler.js';
import Controller from './controllers/controller.js';
import Parser from './parser/parser.js';
import createPromiseCapability from './utils/promise_util.js';
import createMediaSource from './utils/mediasource_util.js';
/**
* @class Player
*/
export default class Player {
/**
* @constructs Player
*/
constructor(video, audio) {
this._video = video;
this._audio = audio;
this._mediaSource = createMediaSource();
this._readyCapability = createPromiseCapability();
this._ready = _readyCapability.promise;
this._src;
// Set a fake duration for the video.
this._duration = 112;
this._eventHandler = new EventHandler(video, audio);
this._controller;
this._parser;
}
/**
* @return {Number} Duration of the video.
*/
get duration() {
return this._duration
}
/**
* @return {Boolean} Player in play state.
*/
get isPlaying() {
return this._eventHandler.isPlaying;
}
/**
* @return {Boolean} Player in pause state.
*/
get isPaused() {
return this._eventHandler.isPaused;
}
/**
* @return {Number} Current current time of player.
*/
get currentTime() {
return this.video.currentTime;
}
/**
* @return {Promise}
* Resolved when player is ready for playback.
*/
get onReady() {
return this._ready;
}
/**
* @return {Null}
* Starts playback.
*/
play() {
this._video.play();
this._audio.play();
}
/**
* @return {Null}
* Pauses playback.
*/
pause() {
this._video.play();
this._audio.play();
}
/**
* @return {Null}
* @params {String} New source for video.
*/
load(src) {
this._src = src;
this._parser = new Parser(src);
this._parser.parse().then(() => {
this._video.src = window.URL.createObjectURL(this._mediaSource);
this._audio.src = window.URL.createObjectURL(this._mediaSource);
this._controller = new Controller(this._mediaSource, this._parser);
this._readyCapability.resolve();
});
}
/**
* @return {Null}
* Unload the current video source.
*/
unload() {
this._src = null;
window.URL.revokeObjectURL(this._video.src);
window.URL.revokeObjectURL(this._audio.src);
this._video.src = null;
this._audio.src = null;
}
/**
* @return {Null}
* Destroy current instance of the player.
*/
destroy() {
// Remove pointer to object to free memory.
this._mediaSource = null;
this._video = null;
this._audio = null;
}
}