-
Notifications
You must be signed in to change notification settings - Fork 0
/
hls-player.mjs
48 lines (39 loc) · 1.14 KB
/
hls-player.mjs
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
import HLS from 'https://esm.sh/[email protected]';
export class HLSPlayer extends HTMLVideoElement {
static get observedAttributes() {
return ['src'];
}
constructor() {
super();
}
attributeChangedCallback(atb, current, newValue) {
if (atb !== 'src') return;
if (
!newValue || // if value is not empty
current === newValue || // ignore if the value is the same
-1 < newValue.indexOf('blob:http') // ignore hls.js changing the src
) {
return;
}
if (this.hls) {
this.hls.destroy();
}
this.hls = new HLS({
maxLiveSyncPlaybackRate: 1.5,
});
this.hls.loadSource(newValue);
this.hls.attachMedia(this);
this.play();
}
onTabResume(e) {
if (document.visibilityState !== 'visible') return;
this.currentTime = this.duration;
}
connectedCallback() {
document.addEventListener('visibilitychange', this.onTabResume.bind(this));
}
disconnectedCallback() {
document.removeEventListener('visibilitychange', this.onTabResume.bind(this));
}
}
window.customElements.define('hls-player', HLSPlayer, { extends: 'video' });