-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.html
103 lines (79 loc) · 2.71 KB
/
player.html
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
<script src="https://cdnjs.cloudflare.com/ajax/libs/hls.js/0.7.5/hls.light.min.js"></script>
<template id="player">
<style>
video {
width: 100%;
height: 100%;
}
</style>
<h1 class="title"></h1>
<video controls muted>
</template>
<script>
(function () {
var importer = document.currentScript.ownerDocument;
// Cannot extend a customized element, such as HTMLVideoElement or HTMLMediaElement.
// https://bugs.chromium.org/p/chromium/issues/detail?id=619062
class HlsVideo extends HTMLElement {
// Observe the following attributes for changes.
static get observedAttributes () {
return ['src', 'video-title'];
}
/**
* Called when the element is created or upgraded.
*/
constructor () {
var template, content, root;
// Always call super first in constructor.
super();
// Get the template in this import.
template = importer.querySelector('#player');
// Create a deep copy of the template, which includes all children
// 2nd argument specifies deep copy.
content = document.importNode(template.content, true);
// Add the copied template to this shadow root.
root = this.createShadowRoot();
root.appendChild(content);
this.elTitle = root.querySelector('h1');
this.elVideo = root.querySelector('video');
this.hlsI = new Hls();
this.hlsI.on(Hls.Events.MANIFEST_PARSED, this.elVideo.play.bind(this.elVideo));
}
/**
* Called when an attribute is changed, appended, removed, or replaced on the element.
* Only called for observed attributes.
*
* @param {string} attr - The name of the attribute that was changed.
* @param {string} oldValue - The previous value of the attribute.
* @param {string} newValue - The new (current) value of the attribute.
*/
attributeChangedCallback (attr, oldValue, newValue) {
if (attr === 'src') {
this.play(newValue);
}
if (attr === 'video-title') {
this.elTitle.textContent = newValue;
}
}
/**
* Plays the given HLS video stream.
*
* @param {string} url - The URL of the m3u8 file.
*/
play (url) {
if (!this.elVideo.paused) {
this.reset();
}
this.hlsI.attachMedia(this.elVideo);
this.hlsI.loadSource(url);
}
/**
* Resets the HLS instance.
*/
reset () {
this.hlsI.detachMedia();
}
};
customElements.define('hls-video', HlsVideo);
})();
</script>