Sub topic 6
Controlling Playback with Attributes
This chapter assumes you can write basic HTML, know how audio and video load a file with src, and have seen a source element. Boolean attributes (ones that are on when present) control most playback behavior. You will see what each attribute does and which ones to combine for common use cases.
The attributes
| Attribute | Effect | Notes |
|---|---|---|
| `controls` | Shows the browser's built-in player UI | Without it, the media is invisible (audio) or unplayable by the user (video) unless you script it. |
| `autoplay` | Starts playback as soon as possible | Often blocked by browser policy. |
| `loop` | Restarts at the end | The `ended` event does not fire while looping. |
| `muted` | Starts silent | Required for most autoplay. |
| `preload` | Hints how much to fetch up front | Values: `none`, `metadata`, `auto`. |
| `playsinline` | Plays inside the page on iOS | Without it, older iOS Safari may open full screen. |
preload is a hint, not a command. The browser may ignore it, for example on a metered connection.
nonefetches nothing until the user plays. This saves bandwidth, but the duration is unknown.metadatafetches duration and dimensions only. It is a good default for pages with many players.autolets the browser download as much as it wants. Use it when playback is very likely.
Choosing attributes by use case
Start from what the user expects, then add only what you need.
A podcast episode should let the user decide when to listen:
<audio controls preload="none" src="podcast-episode.mp3">
<a href="podcast-episode.mp3">Download the episode</a>
</audio>A tutorial video needs controls and a cheap initial load:
<video controls preload="metadata" width="640" src="clip.mp4"></video>A decorative background video has no controls. It must be muted so browsers allow it to autoplay:
<video autoplay muted loop playsinline src="background.mp4"></video>Common errors
- Writing
muted="false". A boolean attribute is on whenever it is present, whatever its value. To turn it off, remove the attribute. - Using
autoplaywithoutmuted. Browsers usually block it, and nothing tells you why unless you handle the promise fromplay(). - Omitting
controlsandautoplaytogether. The user then has no way to start the media. - Setting
preload="auto"on every player. Ten players on a page can trigger ten large downloads. - Autoplaying sound-free video with important content. Motion that starts without a way to pause it is a problem for some users. Prefer
controlsfor anything that carries information.
Exercise
Exercise
You are embedding a 40-minute lecture video on a page that shows eight lectures. You want fast page loads and you want users to start each video themselves. Which attributes do you set on each video element, and why not preload="auto"?
Show answer
Set controls so users can start playback, and preload="metadata" (or none) so each video fetches only its duration and dimensions. preload="auto" could make the browser download large parts of all eight videos, which wastes bandwidth when most will never be played. Do not set autoplay, because the user should start each lecture.
Exercise
A teammate wrote <video autoplay muted="false" loop src="bg.mp4"></video> and expects sound. Why is there no sound, and what must change for the video to play on iOS without opening full screen?
Show answer
muted="false" still counts as muted, because the attribute is present. Remove muted to allow sound, though autoplay with sound will probably be blocked. For inline playback on iOS, add playsinline.