Sub topic 14
Building custom controls
This chapter assumes you know the HTMLMediaElement API, media events, and how play() returns a promise. You also need basic keyboard behavior for form controls.
When to build your own
The built-in controls UI is keyboard accessible and works on every device. Build custom controls only when you need a specific design or extra behavior, such as a chapter list. If you replace the native UI, you take over its accessibility.
Start with real elements
Remove the controls attribute. Then build the interface from native elements: a <button> for play and pause, and an <input type="range"> for seeking. Native elements give you focus, Space and Enter activation, and arrow-key adjustment for free.
<video id="player" src="clip.mp4" preload="metadata"></video>
<div role="group" aria-label="Video controls">
<button type="button" id="toggle">Play</button>
<label for="seek">Seek</label>
<input type="range" id="seek" min="0" max="0" step="0.1" value="0">
</div>Let events drive the interface
Do not update the button text in the click handler. Playback can also start or stop from other sources: the keyboard, a phone's lock screen, a blocked autoplay, or another script. Listen to the media events instead, so the interface always reflects the real state.
const video = document.querySelector('#player');
const toggle = document.querySelector('#toggle');
const seek = document.querySelector('#seek');
function format(seconds) {
const m = Math.floor(seconds / 60);
const s = String(Math.floor(seconds % 60)).padStart(2, '0');
return `${m}:${s}`;
}
toggle.addEventListener('click', async () => {
if (video.paused) {
try {
await video.play();
} catch (err) {
console.error('Playback was blocked or failed:', err.name);
}
} else {
video.pause();
}
});
video.addEventListener('play', () => { toggle.textContent = 'Pause'; });
video.addEventListener('pause', () => { toggle.textContent = 'Play'; });
video.addEventListener('loadedmetadata', () => {
seek.max = video.duration;
});
video.addEventListener('timeupdate', () => {
seek.value = video.currentTime;
seek.setAttribute(
'aria-valuetext',
`${format(video.currentTime)} of ${format(video.duration)}`
);
});
seek.addEventListener('input', () => {
video.currentTime = Number(seek.value);
});The play() call is awaited in a try/catch. If the browser rejects it, the play event never fires, so the button correctly stays on "Play".
Accessibility checklist
- Use
<button>and<input>, not clickable<div>elements. A div needs a role, a tabindex, and key handlers to match. - Give every control a name, either visible text, a
<label>, oraria-label. - Keep a button's name stable, or change its visible text as above. Do not change the text and also set
aria-pressed, because screen readers would then announce the state twice. - Set
aria-valuetexton the slider so it announces "0:12 of 1:30" instead of a raw number of seconds. - Keep a visible focus outline.
- Keep captions working. A
<track>element still displays text tracks whencontrolsis absent, but a caption on/off toggle is now your job.
Common errors
- Setting
seek.maxbefore metadata loads.video.durationisNaNuntilloadedmetadata. - Updating the interface only on click. The state then drifts out of sync with the video.
- Ignoring the
play()rejection, which produces an unhandled promise error.
Exercise
Why should the button text change in the play and pause event handlers rather than in the click handler?
Show answer
Playback can start or stop without a click on your button, for example through a media key or a script. It can also fail, because play() may reject. Events report what actually happened, so the interface stays correct.