<video> element on the page, load hls.js, and call hls.loadSource(url) and hls.attachMedia(video); where hls.js is not supported, set video.src to the M3U8 URL for native playback. The stream must be served over HTTPS with CORS headers. The Embed Player Generator writes this code for you as self-hosted HTML.Most browsers cannot play an M3U8 URL from a bare <video> tag in a way you can rely on. The standard solution is hls.js, an open-source library that reads the HLS playlist and feeds the segments to the browser's decoder. This page gives a complete working example and the pieces usually added next.
A minimal working page
Download hls.min.js from the hls.js release (or npm install hls.js and copy dist/hls.min.js) to your own server. Then:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>HLS player</title>
<style>
.player { position: relative; max-width: 960px; aspect-ratio: 16 / 9; background: #000; }
.player video { position: absolute; inset: 0; width: 100%; height: 100%; }
</style>
</head>
<body>
<div class="player">
<video id="video" controls playsinline
poster="https://example.com/poster.jpg"></video>
</div>
<script src="/js/hls.min.js"></script>
<script>
const video = document.getElementById('video');
const src = 'https://example.com/hls/master.m3u8';
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(src);
hls.attachMedia(video);
hls.on(Hls.Events.ERROR, (event, data) => {
if (!data.fatal) return;
if (data.type === Hls.ErrorTypes.NETWORK_ERROR) hls.startLoad();
else if (data.type === Hls.ErrorTypes.MEDIA_ERROR) hls.recoverMediaError();
else hls.destroy();
});
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = src; // native HLS, e.g. Safari on older iPhones
}
</script>
</body>
</html>
Hls.isSupported() is true wherever Media Source Extensions exist: Chrome, Edge, Firefox, desktop Safari and recent iOS versions. The else branch covers browsers with native HLS only. Test hls.js first: Chrome and Edge 142 and later also report native support, and putting the native branch first would hand them a player without the quality control and error recovery shown here.
The error handler matters for live streams. A fatal network error is retried with startLoad() and a decoding error with recoverMediaError(); without it, one failed segment ends playback.
Generate this code for your stream Test the URL first
Responsive size, autoplay and poster
Responsive 16:9. The wrapper's aspect-ratio: 16 / 9 reserves the space before the video loads, which prevents the page from jumping. The wrapper follows the width of its container up to max-width. Every current browser supports the property.
Autoplay. Browsers allow autoplay only without sound. Use all three attributes:
<video id="video" controls autoplay muted playsinline></video>
muted satisfies the autoplay policy and playsinline stops iPhones from switching to fullscreen. With hls.js, also start playback once the manifest is parsed and catch the refusal:
hls.on(Hls.Events.MANIFEST_PARSED, () => {
video.play().catch(() => { /* blocked: wait for a click */ });
});
Never rely on autoplay with sound; MDN's autoplay guide lists the conditions.
Poster. The poster image shows until the first frame is decoded. Use a 16:9 image at the display size of the player, not the full source resolution.
Quality selector
hls.js picks a quality level automatically from measured bandwidth. To let viewers override it, add <select id="quality"></select> below the player and fill it from hls.levels:
hls.on(Hls.Events.MANIFEST_PARSED, () => {
const select = document.getElementById('quality');
select.add(new Option('Auto', -1));
hls.levels.forEach((level, index) => {
select.add(new Option(level.height + 'p', index));
});
select.onchange = () => { hls.currentLevel = Number(select.value); };
});
Setting currentLevel switches immediately and flushes the buffer; nextLevel switches at the next segment without a pause. -1 returns to automatic selection. A stream with one variant has nothing to select; the M3U8 tester shows how many your manifest has.
React component
import { useEffect, useRef } from 'react';
import Hls from 'hls.js';
export default function HlsPlayer({ src, poster }) {
const ref = useRef(null);
useEffect(() => {
const video = ref.current;
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource(src);
hls.attachMedia(video);
return () => hls.destroy();
}
if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = src;
}
}, [src]);
return <video ref={ref} poster={poster} controls playsInline />;
}
The cleanup function is essential. Without hls.destroy() every change of src, and every double mount in development Strict Mode, leaves an instance downloading segments in the background.
video.js as an alternative
video.js is a complete player with its own skinnable controls, a plugin system and a built-in HLS engine, at a larger download size than hls.js. Choose it when you need a consistent interface across browsers, playlists or advertising plugins.
<link href="/css/video-js.min.css" rel="stylesheet">
<video id="player" class="video-js vjs-fluid" controls playsinline>
<source src="https://example.com/hls/master.m3u8" type="application/x-mpegURL">
</video>
<script src="/js/video.min.js"></script>
<script>videojs('player');</script>
The class vjs-fluid makes the player responsive.
WordPress
Add a Custom HTML block and paste the <div class="player">, both <script> tags and the CSS in a <style> tag. Three things go wrong regularly:
- WordPress strips
<script>from posts saved by users without theunfiltered_htmlcapability, which on multisite is everyone except super admins. Save the post as an administrator. - Optimisation plugins that defer or combine JavaScript can run the inline script before hls.js. Exclude both from optimisation, or wrap the inline code in
window.addEventListener('load', …). - Two players on one page need different
idvalues.
Upload hls.min.js to your theme or the media library and reference that URL.
What the stream's server must allow
- CORS. Playlists, segments and keys must be sent with
Access-Control-Allow-Originfor your site's origin. Check with the CORS checker; server configuration is in the CORS guide. - HTTPS. A page on HTTPS cannot load an HTTP stream.
- Permission. Embed streams you own or are licensed to show.
Performance and accessibility
- Load nothing until needed. With
new Hls({ autoStartLoad: false })andhls.startLoad()in aplayevent listener, a visitor who never presses play downloads no video. - Lazy-load hls.js. For a player below the fold, load the library with
import('hls.js')or an injected script tag on first click, or when anIntersectionObserverreports the player in view. - Cap quality to the player size.
capLevelToPlayerSize: truestops a 480-pixel-wide player from fetching 1080p. - Captions. WebVTT subtitles declared in the manifest appear in the native captions menu. For a separate file add
<track kind="captions" src="/captions/en.vtt" srclang="en" label="English">. - Keyboard. The native
controlsare keyboard-operable and announced by screen readers. Custom controls must be real<button>elements with text labels, reachable by Tab. - Label the video with
aria-labelor a visible heading, and keep autoplaying video muted with a visible pause control.