This repository was archived by the owner on Mar 13, 2025. It is now read-only.
forked from mingxinstar/react-hls
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathApp.tsx
More file actions
144 lines (129 loc) · 3.41 KB
/
Copy pathApp.tsx
File metadata and controls
144 lines (129 loc) · 3.41 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
import Hls from 'hls.js';
import React, { useState, useRef } from 'react';
import HlsPlayer from '../../src';
function App() {
const playerRef = useRef<HTMLVideoElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const [hlsUrl, setHlsUrl] = useState(
'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8'
);
const [destroy, setDestroy] = useState(false);
const [HLSInstance, setHLSInstance] = useState<Hls>();
const [displayHLSPrperties, setDisplayHLSPrperties] = useState(false)
const [selectedQuality, setSelectedQuality] = useState<number>();
function _handleEnter(e: React.KeyboardEvent) {
if (e.keyCode === 13) {
setHlsUrl(inputRef?.current?.value ?? '');
}
}
function _handleDestroyClick() {
setDestroy(true);
}
function _handleToggleControls() {
if (playerRef?.current?.hasAttribute('controls')) {
playerRef.current.removeAttribute('controls');
} else {
playerRef?.current?.setAttribute('controls', 'true');
}
}
function _handleHLSInstanceClick() {
setDisplayHLSPrperties(!displayHLSPrperties)
}
function getHLSInstance(hlsInstance: Hls) {
setHLSInstance(hlsInstance);
}
function handleQuality(e: React.ChangeEvent<HTMLSelectElement>) {
setSelectedQuality(Number(e.target.value));
if(HLSInstance) HLSInstance.currentLevel = Number(e.target.value);
}
return (
<div>
<div
style={{
margin: '0 0 20px',
}}
>
<label
style={{
display: 'block',
marginBottom: 10,
}}
htmlFor="url-input"
>
hls url :{' '}
</label>
<input
ref={inputRef}
id="url-input"
type="text"
defaultValue={hlsUrl}
onKeyUp={_handleEnter}
style={{
width: '100%',
height: '30px',
lineHeight: '30px',
fontSize: '16px',
color: '#333',
}}
/>
</div>
{!destroy ? (
<HlsPlayer
loop={true}
width="100%"
height="auto"
autoPlay
playerRef={playerRef}
src={hlsUrl}
getHLSInstance={getHLSInstance}
/>
) : null}
<br />
<button
style={{
padding: '5px 10px',
}}
onClick={_handleDestroyClick}
>
Destroy Video
</button>
<button
style={{
padding: '5px 10px',
}}
onClick={_handleToggleControls}
>
Toggle Controls (via custom ref)
</button>
<button
style={{
padding: '5px 10px',
}}
onClick={_handleHLSInstanceClick}
>
Get HLS Instance
</button>
{displayHLSPrperties && (
<div
style={{
padding: "5px 10px",
border: "1px solid black",
margin: "15px 15px",
}}
>
<h3>HLS Details</h3>
<p>Total Levels: {HLSInstance?.levels?.length}</p>
<div>
<p>Select Quoality</p>
<select value={selectedQuality} defaultValue={`${HLSInstance?.currentLevel}`} onChange={handleQuality}>
{HLSInstance?.levels?.map((level, index) => {
return <option value={`${index}`}>{level.bitrate}</option>
})}
</select>
</div>
</div>
)}
</div>
);
}
export default App;