hls / index.html
AIBOT111's picture
Update index.html
535c3ef verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple M3U8 Player with Quality Selection</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
flex-direction: column;
}
video {
max-width: 100%;
max-height: 80%;
border: 1px solid #ccc;
margin-bottom: 10px;
}
select {
padding: 5px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<video id="video" controls></video>
<select id="qualitySelector">
<option value="">Select Quality</option>
</select>
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script>
const video = document.getElementById('video');
const qualitySelector = document.getElementById('qualitySelector');
const videoUrl = 'https://vhoichoi.viewlift.com/Renditions/20241028/1730121916094_maaza_grand_pujo_parikrama_platform/hls/1730121916094_maaza_grand_pujo_parikrama_platform.m3u8'; // Replace with your M3U8 URL
if (Hls.isSupported()) {
const hls = new Hls();
// Attach event listeners to track levels (quality options)
hls.on(Hls.Events.MANIFEST_PARSED, (event, data) => {
// Populate the quality selector dropdown
data.levels.forEach((level, index) => {
const option = document.createElement('option');
option.value = index;
option.text = `${level.height}p (${(level.bitrate / 1000).toFixed(2)} kbps)`;
qualitySelector.appendChild(option);
});
});
hls.on(Hls.Events.LEVEL_SWITCHED, (event, data) => {
console.log('Switched to level:', data.level);
qualitySelector.value = data.level; // Update dropdown with selected quality
});
// Listen for the user selecting a new quality level
qualitySelector.addEventListener('change', (event) => {
const selectedLevel = event.target.value;
if (selectedLevel !== "") {
hls.startLevel = parseInt(selectedLevel);
console.log('User selected level:', selectedLevel);
}
});
// Load the stream
hls.loadSource(videoUrl);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, function() {
console.log('Manifest parsed');
});
hls.on(Hls.Events.ERROR, function(event, data) {
if (data.fatal) {
switch (data.fatal) {
case Hls.ErrorTypes.NETWORK_ERROR:
console.error('Network error');
break;
case Hls.ErrorTypes.MEDIA_ERROR:
console.error('Media error');
break;
case Hls.ErrorTypes.OTHER_ERROR:
console.error('Other error');
break;
default:
console.error('Fatal error');
break;
}
}
});
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
// If the browser supports HLS natively (like Safari)
video.src = videoUrl;
} else {
console.error('HLS.js is not supported in your browser.');
}
</script>
</body>
</html>