CKTIS / JavaScript
HTMLMediaElement: time, volume, and playback events
HTMLMediaElement supplies the shared API for audio and video elements. Read playback state, change position and muting, and update the interface from events rather than assuming a playback command succeeded immediately.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
media.currentTime = seconds; media.volume = 0.5; media.muted = true; media.addEventListener("timeupdate", render);HTMLMediaElement is the shared interface for audio and video elements. It is a Web API defined by HTML, not a feature of a particular ECMAScript edition. Having Node 24 does not establish browser support. The core interface is widely available, but volume has compatibility limitations; check target browsers and devices.
HTMLMediaElement.currentTime gives the position in seconds. When readyState is HAVE_NOTHING, as on the element without a source in the example, assigning a value stores the default playback start position instead of initiating a seek. In other states, assigning a value requests a seek within the available timeline. HTMLMediaElement.duration is read-only: seconds, NaN without available data, or Infinity for indefinite duration. Calculate percentage progress only for a finite, positive duration. HTMLMediaElement.volume accepts values from 0 to 1. HTMLMediaElement.muted independently silences audio without changing the stored volume.
The read-only properties HTMLMediaElement.paused and HTMLMediaElement.ended indicate paused state and completed playback respectively; they are not interchangeable. paused === false does not guarantee progress, for example during buffering. HTMLMediaElement.error holds null or a MediaError object. The play and pause events report state changes, whereas the play() and pause() methods issue commands. Handle rejection of the promise from play(), for example when autoplay policy blocks playback.
loadedmetadata signals loaded metadata, durationchange a duration update, and emptied an emptied resource state—refresh the interface then too. Use timeupdate to refresh position, but not as a frame clock: its frequency depends on load. volumechange covers changes to volume or muted; ended signals the end of playback. The error event can report not only a resource-loading failure but also a fatal decoding error after playback has begun. Read current properties inside event handlers.
The example creates an element without a source, registers events, and prints its initial state. It neither plays a file nor tests decoding. NaN is deliberately represented as text so JSON serialization preserves that distinction.
Examples
const media = document.createElement("audio");
const events = [
"timeupdate", "loadedmetadata", "durationchange", "emptied",
"play", "pause", "volumechange", "ended", "error"
];
function snapshot() {
return JSON.stringify({
currentTime: media.currentTime,
duration: Number.isNaN(media.duration) ? "NaN" : String(media.duration),
volume: media.volume,
muted: media.muted,
paused: media.paused,
ended: media.ended,
error: media.error === null ? null : media.error.code
});
}
for (const type of events) {
media.addEventListener(type, () => console.log(type, snapshot()));
}
console.log(snapshot());Result
{"currentTime":0,"duration":"NaN","volume":1,"muted":false,"paused":true,"ended":false,"error":null}The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.
Sources
- HTMLMediaElement - Web APIs | MDN
- HTMLMediaElement: currentTime property - Web APIs | MDN
- HTMLMediaElement: duration property - Web APIs | MDN
- HTMLMediaElement: volume property - Web APIs | MDN
- HTMLMediaElement: timeupdate event - Web APIs | MDN
- HTMLMediaElement: volumechange event - Web APIs | MDN
- HTMLMediaElement: paused property - Web APIs | MDN
- HTMLMediaElement: ended property - Web APIs | MDN
- HTMLMediaElement: error property - Web APIs | MDN
- HTMLMediaElement: play() method - Web APIs | MDN