HTMLMediaElement: The Remote Control for Browser Media

HTMLMediaElement is the shared "remote control" API for <audio> and <video> tags, letting you programmatically play, pause, and seek. Use it for custom players or syncing animations. Footgun: Browsers often block autoplay, so never assume it will work.
Why it exists
Before a standard API, controlling audio and video with JavaScript was a mess of browser-specific hacks. The HTMLMediaElement API was created to provide a single, consistent interface for programmatically managing media playback, regardless of whether the element is <audio> or <video>. This simplifies building rich media experiences.
The mental model
Imagine a standard TV remote control. It has buttons for play, pause, volume, and mute, and a display showing the current time. The HTMLMediaElement API is the JavaScript equivalent of that remote. It's not the video or audio itself, but the interface you use to interact with it. Both <audio> and <video> elements are "TVs" that respond to this same standard "remote".
How it works
When you get a reference to an <audio> or <video> element in JavaScript (e.g., with document.querySelector('video')), the returned object inherits from HTMLMediaElement. This gives you access to all its properties and methods. You can read properties like .paused (a boolean) or .duration (in seconds). You can also set properties like .currentTime to seek to a specific point or .volume to adjust loudness. Calling methods like .play() and .pause() directly controls playback.
When to use it
Use this API whenever you need to go beyond the browser's default media controls. Common use cases include building a completely custom video player UI with your own buttons and scrub bar, automatically pausing a video when a user scrolls it out of view, or synchronizing on-screen text with specific timestamps in an audio track. It's also used to check how much of a media file has downloaded using the .buffered property.
When not to use it
If you just need to embed a simple video and the default browser controls are sufficient, you don't need to interact with this API directly; simply using the <video controls> tag is enough. For complex streaming like adaptive bitrate (DASH/HLS), you'll typically use a higher-level library like HLS.js or Shaka Player, which use this API under the hood but manage the complexity for you.
One canonical example
To make a video play when a button is clicked and update a time display:
const video = document.querySelector('video');
const playButton = document.querySelector('#playBtn');
const timeDisplay = document.querySelector('#time');playButton.addEventListener('click', () => {
if (video.paused) {
video.play();
} else {
video.pause();
}
});video.addEventListener('timeupdate', () => {
timeDisplay.textContent = Time: ${Math.floor(video.currentTime)}s;
});Interview question
What is the primary role of the HTMLMediaElement API in web development?
- a.To directly embed and display audio or video content within an HTML document.
- b.To ensure that all embedded media automatically starts playing as soon as the page loads.
- c.To define the specific file formats and codecs used for audio and video content.
- d.To enable developers to create custom playback controls and interact programmatically with media.Correct
Why? this is the answer
The HTMLMediaElement API acts as a 'remote control' for <audio> and <video> elements, providing a unified JavaScript interface for programmatic control like playing, pausing, and seeking. Option B is incorrect because while autoplay is a media feature, browsers often block it, and the API's primary role is control, not guaranteed auto-playback.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on web apis — each one lists the topics its interview covers.
See open roles