How to Autoplay a Video in the Browser
Set up HTML video autoplay with muted, loop, and playsinline. Handle blocked play() calls with NotAllowedError, show a play button, and configure iframe permissions.
Browsers let a muted video autoplay, but they block autoplay with sound until the user has interacted with the site, so a <video autoplay> element with an audible track often sits on its first frame instead of playing.
You add autoplay to a hero video, it works on your laptop, and then a colleague’s iPhone shows a frozen poster. Or the console shows Uncaught (in promise) DOMException: play() failed. Both come from the same autoplay policy, and both have the same fix: the right attributes, plus a script that handles the case where the browser refuses playback anyway.
This guide covers the markup for HTML video autoplay, why muted matters, how to detect a blocked play() call, how to unmute correctly, and how autoplay works inside iframes.
Key Takeaways
- Chrome always allows muted autoplay. Autoplay with sound needs prior interaction with the domain, a high enough Media Engagement Index on desktop, or an installed site.
- Use
autoplay muted loop playsinlinetogether for hero and background videos. On iPhone, a video withoutplaysinlinerequires fullscreen to play. HTMLMediaElement.play()returns a promise that rejects with aDOMExceptionnamedNotAllowedErrorwhen the autoplay policy blocks playback.- Catch the rejection, check
error.name, and show a visible play button. Muted video can still be refused, so the fallback is always needed. - Cross-origin iframes need
allow="autoplay"before they can autoplay with sound.
Why Does HTML Video Autoplay Get Blocked?
Autoplay is blocked when the media would make sound and the user hasn’t yet given the page permission through interaction. Chrome’s autoplay policy says muted autoplay is always allowed. Sound is allowed only when the user has interacted with the domain (a click or tap, for example), when their Media Engagement Index (MEI) has crossed Chrome’s threshold on desktop, or when the user has added the site to their home screen on mobile or installed the PWA on desktop.
The policy is not new. Chrome began enforcing it for <video> and <audio> in Chrome 66 Stable, in April 2018. Other browsers have their own rules, but MDN’s autoplay guide states the common ground plainly: inaudible media are not affected by autoplay blocking.
The Autoplay Attributes: autoplay, muted, playsinline, loop
To autoplay a video in HTML, put autoplay, muted, loop and playsinline together on the <video> element. A hero or background video needs all four boolean attributes. Here is the version that usually fails:
<video src="hero.mp4" autoplay></video>
The file has an audio track and the user hasn’t interacted yet, so Chrome blocks it. On iPhone, it also lacks playsinline. Here is the working version:
<video
src="hero.mp4"
poster="hero.jpg"
autoplay
muted
loop
playsinline
></video>
autoplayasks the browser to start playback as soon as it can.mutedmakes the video silent, which is what the autoplay policy allows.playsinlinekeeps playback inside the page. WebKit’s iOS video policy requires it on iPhone. Without it, the video can only play fullscreen.looprestarts the video when it ends.posteris the image the user sees if playback never starts, so make it a frame that works as a still.
Why Muted Matters (and Why It’s Still Not a Guarantee)
Muting a video is the one setting that every major browser autoplay policy treats as safe, but muted autoplay is still not guaranteed: each browser applies its own rules, and user settings can override them. MDN documents a Firefox preference that, when the user changes it from its default, stops even muted media from autoplaying. That means your code has to handle a refusal even for a silent loop.
| Setup | Chrome desktop | iPhone Safari |
|---|---|---|
Muted, with playsinline | Plays | Plays inline |
Muted, without playsinline | Plays | Fullscreen only, no inline playback |
| Unmuted, no prior interaction | Blocked unless the MEI threshold has been crossed | Blocked |
The Media Engagement Index (MEI) explains why a page can autoplay with sound on one machine and not on another. Chrome stores the score locally per profile, per device and per origin, and doesn’t sync it. A browser profile you use to open your staging site every day can build a high score there, which hides the bug from you while real visitors see a silent, frozen video.
Chrome’s autoplay post says you can inspect the MEI score at about://media-engagement. It also documents two command-line flags for testing. --autoplay-policy=no-user-gesture-required simulates a strongly engaged user, so autoplay is always allowed. To reproduce the blocked case, launch Chrome with --disable-features=PreloadMediaEngagementData, MediaEngagementBypassAutoplayPolicies, which turns off the engagement-based exceptions so autoplay with sound is never allowed.
There is also navigator.getAutoplayPolicy(), which returns "allowed", "allowed-muted" or "disallowed". It isn’t widely supported, so checking the result of play() is still the method you can rely on.
Handling play() and NotAllowedError
HTMLMediaElement.play() returns a promise. When the browser blocks playback, that promise rejects with a DOMException whose name is NotAllowedError. In the catch handler, check error.name === 'NotAllowedError' and show a visible play button. Any other error means the media failed to load or decode, not that autoplay was refused.
<video id="hero" src="hero.mp4" poster="hero.jpg"
autoplay muted loop playsinline></video>
<button id="play-btn" hidden>Play video</button>
<button id="sound-btn">Turn sound on</button>
<script>
const video = document.getElementById('hero');
const playBtn = document.getElementById('play-btn');
const soundBtn = document.getElementById('sound-btn');
function tryPlay() {
return video.play().then(() => {
playBtn.hidden = true;
}).catch((error) => {
if (error.name === 'NotAllowedError') {
playBtn.hidden = false; // blocked by autoplay policy
} else {
console.error(error); // load or decode problem
}
});
}
tryPlay();
playBtn.addEventListener('click', tryPlay);
soundBtn.addEventListener('click', () => {
video.muted = false;
});
</script>
The script keeps the autoplay attribute and also calls play() itself, because the explicit call is the only way to get a promise you can inspect. There’s no promise !== undefined guard because that check only existed for old browsers whose play() returned nothing. The play button reuses tryPlay, so the second attempt, which now runs from a click, has its rejection handled too. Don’t show pause controls or any “playing” state until the promise resolves.
Don’t retry play() on an interval or a timer. A blocked call stays blocked until the user interacts with the page, so a timer just keeps failing. If you start playback from an IntersectionObserver, that play() call needs the same catch.
Without this handling, the user experience is easy to miss. A session replay of a page with an unhandled rejection shows the user stopped on a static poster frame, never pressing play because nothing tells them the video is supposed to move.
Unmuting the Video in a Click Handler
To unmute an autoplaying video, set video.muted = false inside a click handler. The change then happens as a direct response to a user gesture, which the HTML Standard’s user activation model recognizes. The sound-btn listener above does exactly this.
Don’t start muted and then unmute on a setTimeout. An unmute that doesn’t come from a user gesture is still subject to the autoplay policy, and the browser pauses the video.
Autoplay in Iframes With allow=“autoplay”
A cross-origin iframe can autoplay with sound only if the parent page grants permission with allow="autoplay". The parent can only pass on a permission it already has. Same-origin iframes are allowed by default, because the autoplay Permissions-Policy directive defaults to self.
<iframe src="https://player.example.com/demo" allow="autoplay"></iframe>
<!-- autoplay and fullscreen together -->
<iframe src="https://player.example.com/demo" allow="autoplay; fullscreen"></iframe>
If the parent page has to tell the embedded player to unmute after a click, you need cross-document messaging with postMessage().
Conclusion
Reliable video autoplay comes down to two things: markup the browser autoplay policy accepts (autoplay muted loop playsinline) and a script that expects refusal by catching NotAllowedError and showing a real play button. Test in a fresh browser profile rather than your daily one so the engagement score doesn’t hide the blocked path. Autoplaying videos start downloading when the page loads, so keep them short and compress them before you ship.
FAQs
Why does a muted video with playsinline still not autoplay on some iPhones?
Low Power Mode is the usual cause. When an iPhone or iPad is in Low Power Mode, Safari does not autoplay video, even with muted and playsinline set, and play() rejects with NotAllowedError. A page cannot turn this off. The fix is the same fallback used for any blocked autoplay: catch the rejection, keep the poster visible, and show a play button that calls play() from a tap.
What other errors can video.play() reject with besides NotAllowedError?
The two common ones are NotSupportedError and AbortError. NotSupportedError means the media source is in a format the browser cannot play. AbortError means the play request was interrupted before playback started, for example by a call to pause() or by loading a new source. Neither one means the autoplay policy blocked playback, so neither should trigger the play-button fallback.
How do I autoplay an embedded YouTube video?
Add autoplay=1 and mute=1 to the embed URL and keep allow='autoplay' on the iframe. The browser autoplay policy applies inside the YouTube player too, so muted autoplay is allowed, and without mute=1 the embed usually stays paused. The mute parameter is widely used, but YouTube's player parameter reference does not list it, so test the embed in the browsers you target.