Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
106 lines (89 loc) · 3.45 KB
/
Copy pathindex.html
File metadata and controls
106 lines (89 loc) · 3.45 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Streaming Demo: Good vs Bad Practices</title>
<link href="https://vjs.zencdn.net/8.0.4/video-js.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 30px;
}
h1 {
color: #333;
}
button {
font-size: 16px;
padding: 10px 20px;
margin: 10px;
cursor: pointer;
border: none;
border-radius: 5px;
}
.good-stream {
background-color: #4CAF50;
color: white;
}
.bad-stream {
background-color: #f44336;
color: white;
}
#description {
margin: 20px 0;
}
#audioPlayer {
margin-top: 20px;
display: block;
}
</style>
</head>
<body>
<h1>Streaming Demo: Good vs Bad Practices</h1>
<p id="description">
<br><strong>Bad Practice:</strong> Sends the entire file at once, making it easy to download.
<br><strong>Good Practice:</strong> Sends audio in chunks (HLS), improving security and bandwidth efficiency.
</p>
<button id="badStreamBtn" class="bad-stream">Bad Practice: Entire File</button>
<button id="goodStreamBtn" class="good-stream">Good Practice: Chunked Streaming</button>
<audio id="audioPlayer" class="video-js vjs-default-skin" controls preload="auto" width="600"></audio>
<!-- Video.js lib. -->
<script src="https://vjs.zencdn.net/8.0.4/video.min.js"></script>
<!-- HLS.jsfor browsers without native HLS support -->
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script>
const audioPlayer = document.getElementById('audioPlayer');
const goodStreamBtn = document.getElementById('goodStreamBtn');
const badStreamBtn = document.getElementById('badStreamBtn');
//Bad stream
badStreamBtn.addEventListener('click', () => {
audioPlayer.src = '/bad-stream';
audioPlayer.load();
audioPlayer.play();
console.log('Bad Practice (Entire File) selected.');
});
//Good stream
goodStreamBtn.addEventListener('click', () => {
if (audioPlayer.canPlayType('application/vnd.apple.mpegurl')) {
// Native HLS support
audioPlayer.src = '/good-stream/hls/playlist.m3u8'; //playlist.m3u8 is defualt name for playlist file
// or api call : 'http://localhost:3000/good-stream/hls/playlist.m3u8'
console.log('Native HLS support detected. Using native HLS.');
} else if (Hls.isSupported()) {
// HLS.js fallback for unsupported browsers
const hls = new Hls();
hls.loadSource('/good-stream/hls/playlist.m3u8');
// or api call : 'http://localhost:3000/good-stream/hls/playlist.m3u8'
hls.attachMedia(audioPlayer);
console.log('HLS.js fallback is being used.');
} else {
console.error('This browser does not support HLS streaming.');
}
audioPlayer.load();
audioPlayer.play();
console.log('Good Practice (HLS Chunked Streaming) selected.');
});
</script>
</body>
</html>