-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoStatusDisplay.js
More file actions
193 lines (177 loc) · 5.98 KB
/
VideoStatusDisplay.js
File metadata and controls
193 lines (177 loc) · 5.98 KB
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
import DeferredManager from "./DeferredManager.js";
/**
* @typedef YTObject
* @property {new (...args: any[]) => YTPlayer|undefined} Player
*
* @typedef YTAPI
* @property {()=>void|undefined} onYouTubeIframeAPIReady
* @property {YTObject|undefined} YT
*
* @typedef {Window & typeof globalThis & YTAPI} WindowWithYTAPI
*/
export default class VideoStatusDisplay extends DeferredManager {
/**
* @typedef {Object} YTPlayer
* @property {()=>any} destroy
*/
/**
* @type {YTPlayer|undefined}
*/
youtubePlayer
/** @type {'Playing'|'Paused'|'Ended'|'Buffering'|'Error'|'Unstarted'|'Ready'|'Stopped'|'Initializing'} */
status = 'Initializing';
/**
* @type {(()=>void) | undefined}
*/
_onPlaying
/**
* @type {(()=>void) | undefined}
*/
_onPaused
/**
* Creates a status display for YouTube embed iFrames that is updated by the youtube API
* @param {HTMLElement} musicDetail
* @param {HTMLIFrameElement} [ytEl]
* @param {string} [label]
* @param {string} [iframeElementId]
*/
constructor(musicDetail, ytEl, label = 'Video: ', iframeElementId = 'youtubePlayer') {
super();
this.ytEl = ytEl;
this.musicDetail = musicDetail;
this.label = label;
this.iframeElementId = iframeElementId;
this.window = /** @type {WindowWithYTAPI} */(window);
/** @type {Document} */
this.document = document;
if (this.ytEl) {
this.enableJsApi();
this.prepareCreatePlayer();
}
}
/**
*
* @param {(()=>void) | undefined} value
*/
setOnPlaying(value) {
this._onPlaying = value;
}
/**
*
* @param {(()=>void) | undefined} value
*/
setOnPaused(value) {
this._onPaused = value;
}
/**
*
* @param {HTMLIFrameElement} ytEl
*/
reset(ytEl) {
this.ytEl = ytEl;
this.destroy();
this.resetPromise();
this.enableJsApi();
this.prepareCreatePlayer();
}
destroy() {
this.youtubePlayer?.destroy();
this.youtubePlayer = undefined;
}
/**
* Ensure iframe has enablejsapi=1 so the JS API can control it
*/
enableJsApi() {
if (!this.ytEl) return;
this.src = this.ytEl.getAttribute('src') || '';
const isJsApiEnabled = /(\?|&)enablejsapi=1/.test(this.src);
if (isJsApiEnabled) return;
const separator = this.src.includes('?') ? '&' : '?';
this.ytEl.setAttribute('src', `${this.src}${separator}enablejsapi=1`);
}
/**
* Calls for player creation or to set up the youtube API to call for player creation.
*/
prepareCreatePlayer() {
if (this.window.YT && this.window.YT.Player) {
this.createPlayer();
} else {
// load the IFrame API if not already loaded
this.createScriptTag('https://www.youtube.com/iframe_api');
this.assignToYTAPIHook(this.createPlayer.bind(this));
}
}
/**
* Assigns an additional function to the this.window.onYouTubeIframeAPIReady hook
* @param {()=>any} callback the new function to be assigned
*/
assignToYTAPIHook(callback) {
const prev = this.window.onYouTubeIframeAPIReady;
this.window.onYouTubeIframeAPIReady = () => {
if (typeof prev === 'function') prev();
callback();
};
}
/**
* creates a script element named 'tag' as a child of this.document.head
* @param {string} tagUrl source of the script
*/
createScriptTag(tagUrl) {
const doesTagExist = this.document.querySelector(`script[src="${tagUrl}"]`);
if (doesTagExist) return;
const tag = this.document.createElement('script');
tag.src = tagUrl;
this.document.head.appendChild(tag);
}
/**
* Creates a youtube player to update status
*/
createPlayer() {
if (!this.window.YT || !this.window.YT.Player) return;
// avoid creating multiple players
if (this.youtubePlayer) return;
this.youtubePlayer = new this.window.YT.Player(this.iframeElementId, {
events: {
/** @param {{target:{getPlayerState:Function}}} e */
onReady: (e) => {
// set initial status
try {
const s = e.target.getPlayerState();
this.setStatusText(s, true);
} catch (err) {
this.setStatusText(-1, true);
}
},
/** @param {{data:number}} e */
onStateChange: (e) => {
this.setStatusText(e.data);
}
}
});
}
/**
* Interprets YouTube embed video state and displays it on the this.musicDetail element.
* @param {number} state
* @param {boolean} isReady
*/
setStatusText(state, isReady = false) {
/** @type {'Playing'|'Paused'|'Ended'|'Buffering'|'Error'|'Unstarted'|'Ready'|'Stopped'} */
let status;
switch (state) {
case 1: status = 'Playing'; break; // YT.PlayerState.PLAYING
case 2: status = 'Paused'; break; // YT.PlayerState.PAUSED
case 0: status = 'Ended'; break; // YT.PlayerState.ENDED
case 3: status = 'Buffering'; break; // YT.PlayerState.BUFFERING
case -1: status = isReady ? 'Error' : 'Unstarted'; break; // YT.PlayerState.UNSTARTED
default: status = isReady ? 'Ready' : 'Stopped';
}
this.musicDetail.textContent = `${this.label}${status}`;
this.status = status;
switch (status) {
case 'Ready': this.resolve(this.youtubePlayer); break;
case 'Error': this.reject(); break;
case 'Paused': if (this._onPaused) this._onPaused(); break;
case 'Playing': if (this._onPlaying) this._onPlaying(); break;
}
}
}