Sub topic 8
Why autoplay is restricted
This chapter assumes you know basic HTML attributes, how to select elements and add event listeners in JavaScript, and how promises and async/await work. It builds on the autoplay, muted, and playsinline attributes from the previous chapter on playback attributes. It also prepares you for the later chapters on play() and custom controls.
The problem autoplay causes
Adding the autoplay attribute tells the browser to start playback as soon as enough data is available. That sounds convenient. In practice, early web pages abused it. Pages started loud audio the moment they opened, ads played video nobody asked for, and tabs played sound from places the user could not find.
Browsers now restrict autoplay for four reasons:
- Surprise sound. Sudden audio is startling and can be embarrassing in a quiet office or a meeting.
- Accessibility. Audio that starts on its own talks over screen reader speech, so blind users cannot hear their assistive technology.
- Data and battery. Autoplaying video downloads data and uses power, which costs users on metered or mobile connections.
- Trust. Users blame the browser, not the page, when a tab makes noise.
The restrictions are not part of the HTML specification as fixed rules. The specification lets browsers decide when to block playback, so the exact rules differ between browsers and change over time. Treat the rules below as the common pattern, and test in the browsers you support.
What browsers typically allow
Most browsers use a similar core idea: autoplay is allowed when it is unlikely to annoy the user. In practice, playback starts without a gesture in these cases:
- The media is muted. A video with no audible sound is rarely disruptive, so
mutedautoplay is the most widely permitted case. - The user has interacted with the page. A click, tap, or key press on the page counts as user activation. After that, scripts can usually start audible playback.
- The user has shown interest in the site. Chrome keeps a per-site score of how much you have played media there, called the Media Engagement Index. High-scoring sites may autoplay with sound. Installed web apps can also get more freedom.
Some browsers go further:
| Browser behavior | Typical effect |
|---|---|
| Firefox blocks audible autoplay by default | Muted autoplay works. Audible autoplay needs a user gesture or a site permission. |
| Safari on iOS requires `playsinline` and `muted` for inline autoplay | Without `playsinline`, video may open in the full-screen player. |
| Low power mode on iOS | Safari may block even muted autoplay until the user taps. |
| Users can set per-site permissions | A user may allow or block autoplay for your site, which overrides your code. |
You cannot read these rules from JavaScript, and you cannot override them. Your job is to write pages that behave well whichever way the browser decides.
The decision the browser makes
The diagram shows a simplified version of the decision. Real browsers add more signals, but the shape is the same.
The safe autoplay pattern
If you want decorative video, such as a silent looping background, use the combination browsers most reliably accept:
<video autoplay muted loop playsinline src="background.mp4"></video>Each attribute has a job:
autoplayasks for playback to begin without a click.mutedremoves the audio that would make the request annoying.looprepeats the clip. It is not needed for permission but suits background video.playsinlinekeeps the video in the page on iOS instead of forcing full screen.
Use this only for content that adds nothing the user needs. If the video carries information, give it controls and let the user start it. Even muted motion can be a problem for people with vestibular disorders, so consider pausing decorative video when the user has prefers-reduced-motion set.
Handling a blocked play() call
When you start playback from script, the browser may refuse. play() returns a promise, and a blocked call rejects it with a NotAllowedError. If you ignore the rejection, the console shows an "uncaught in promise" error and your interface may show a pause icon on a video that is not playing.
Always await the promise and handle the failure:
const video = document.querySelector('video');
const button = document.querySelector('#toggle');
button.addEventListener('click', async () => {
if (video.paused) {
try {
await video.play();
} catch (err) {
console.error('Playback was blocked or failed:', err.name);
}
} else {
video.pause();
}
});This call runs inside a click handler, so the click counts as user activation and play() normally succeeds. The catch still matters, because play() can fail for other reasons. For example, the source may be missing, or the format may be unsupported. In those cases the error name is something other than NotAllowedError, such as NotSupportedError.
Trying autoplay and falling back
A common pattern is to try to start playback on load, then show a visible play button if the browser says no:
<video id="hero" src="intro.mp4" playsinline controls></video>
<button id="start" hidden>Play with sound</button>
<script>
const hero = document.querySelector('#hero');
const start = document.querySelector('#start');
async function tryAutoplay() {
try {
await hero.play();
} catch (err) {
if (err.name === 'NotAllowedError') {
start.hidden = false;
} else {
console.error('Playback failed:', err.name);
}
}
}
start.addEventListener('click', async () => {
start.hidden = true;
await hero.play();
});
tryAutoplay();
</script>The video keeps its controls attribute, so the user always has a way to start it. The script is an enhancement, not the only path. If scripting fails or the browser blocks playback, the page still works.
Autoplay inside iframes
An embedded page in an <iframe> has its own rules. A cross-origin frame may not autoplay unless the embedding page delegates the permission with the allow attribute, which uses the Permissions Policy feature:
<iframe src="https://player.example.com/embed/123" allow="autoplay" title="Product demo"></iframe>Without allow="autoplay", the frame's media may be blocked even when the parent page has had user interaction. Delegating the permission lets the embedded player use the same autoplay allowance as the parent. Only delegate it to origins you trust.
Common mistakes
- Expecting
autoplayalone to work. On most browsers,autoplaywithoutmutedis blocked unless the user has interacted with the site. - Removing the
mutedstate from script too early. Settingvideo.muted = falseon a video that was allowed to play silently can cause the browser to pause it, depending on the browser and on whether the user has interacted. Unmute in response to a click. - Forgetting
playsinlineon iOS. The video may refuse to autoplay inline or may jump to full screen. - Not testing with a fresh profile. Your own browser may carry a high engagement score or a saved permission, so autoplay works for you and fails for visitors. Use a private window or a clean profile.
- Hiding the controls. If autoplay fails and there are no controls, the user sees a frozen frame with no way to continue.
- Assuming a result. Treat every
play()call as something that can fail.
Summary of the rules
| Situation | Likely result |
|---|---|
| `autoplay muted playsinline` video | Usually plays. |
| `autoplay` with sound, no prior interaction | Usually blocked. |
| `play()` called inside a click handler | Usually allowed. |
| `play()` called on page load with sound | Usually rejected with `NotAllowedError`. |
| Cross-origin iframe without `allow="autoplay"` | May be blocked. |
| Low power mode or a per-site user setting | May block even muted playback. |
Exercise
A page has <video autoplay src="promo.mp4" controls></video> with a soundtrack. It plays on your development machine but stays paused for most visitors. Give two likely reasons, and describe two changes that make the page behave well for everyone.
Show answer
Reasons: the video is not muted, so browsers block it without user activation, and your own browser may have a high engagement score or a saved site permission that visitors lack. Changes: if the video should start silently, add muted and playsinline. If it needs sound, drop autoplay, keep controls, and start playback from a click handler, where play() is awaited inside try/catch and a NotAllowedError shows a visible play button. Test in a private window to see the visitor experience.