This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
85 lines (71 loc) · 2.11 KB
/
index.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
import CustomVideoElement from 'custom-video-element';
import shaka from 'shaka-player';
function onErrorEvent(event) {
// Extract the shaka.util.Error object from the event.
onError(event.detail);
}
function onError(error) {
// Log the error.
console.error('Error code', error.code, 'object', error);
}
class ShakaVideoElement extends CustomVideoElement {
constructor() {
super();
this.player = null;
if (shaka.Player.isBrowserSupported()) {
this.player = new shaka.Player(this.nativeEl);
// Listen for error events.
this.player.addEventListener('error', onErrorEvent);
} else {
// This browser does not have the minimum set of APIs we need.
console.error('Browser does not support Shaka Player');
}
}
get src() {
// Use the attribute value as the source of truth.
// No need to store it in two places.
// This avoids needing a to read the attribute initially and update the src.
return this.getAttribute('src');
}
set src(val) {
// If being set by attributeChangedCallback,
// dont' cause an infinite loop
if (val !== this.src) {
this.setAttribute('src', val);
}
}
async load() {
if (!this.src) {
this.player.unload();
} else {
// Try to load a manifest.
// This is an asynchronous process.
try {
await this.player.load(this.src);
} catch (e) {
// onError is executed if the asynchronous load fails.
onError(e);
}
}
}
connectedCallback() {
if (this.player && this.src) {
this.load();
}
// Not preloading might require faking the play() promise
// so that you can call play(), call load() within that
// But wait until MANIFEST_PARSED to actually call play()
// on the nativeEl.
// if (this.preload === 'auto') {
// this.load();
// }
}
disconnectedCallback() {
this.player && this.player.unload();
}
}
if (!window.customElements.get('shaka-video')) {
window.customElements.define('shaka-video', ShakaVideoElement);
window.ShakaVideoElement = ShakaVideoElement;
}
export default ShakaVideoElement;