CKTIS / JavaScript
Media playback state and time: paused, muted, currentTime, and duration
Read pause and mute states separately, set a playback position in seconds, and distinguish a known duration from `NaN` and `Infinity`.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
media.paused // boolean, read-only
media.muted // boolean, read/write
media.muted = true // mute the audio output
media.currentTime // number in seconds, read/write
media.currentTime = 12.5 // request a position on the media timeline
media.duration // number in seconds, read-only; may be NaN or InfinityHTMLMediaElement.paused/muted/currentTime/duration groups four instance properties available on audio and video elements. They belong to the web platform, not ECMAScript itself, so don't assume they're available in a runtime without the DOM.
paused is a read-only boolean. A new element starts with paused === true. Call pause() and play() rather than assigning to paused. The promise returned by play() can reject, for example if the browser blocks autoplay. paused === false alone doesn't mean the position is advancing: the element might be waiting for data.
muted is a read/write boolean for the current audio mute state. media.muted = true mutes the output; false unmutes it. Muting isn't pausing or setting volume to zero. defaultMuted reflects the HTML muted attribute, which specifies the default rather than the current mute state.
currentTime reads the playback position on the media timeline in seconds, including fractions. Before metadata is available, assigning it stores a requested start position; once metadata is available, assigning it initiates a seek. NaN and infinities cause a TypeError. The actual position may differ from the requested one: check the seekable ranges, especially for live streams, and use seeked to detect when a seek completes.
duration is read-only: a finite value is the end time in seconds on the media timeline (the length if the timeline starts at zero). NaN means the duration isn't available yet; Infinity means the media has no known end. Read it at loadedmetadata and refresh it on durationchange. For a zero-based timeline, calculate currentTime / duration only when Number.isFinite(duration) && duration > 0.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Media playback state and time</title>
</head>
<body>
<script>
const media = document.createElement("video");
console.log(`currentTime=${media.currentTime}`);
media.muted = true;
media.currentTime = 2.5;
console.log(
`paused=${media.paused}, muted=${media.muted}, ` +
`currentTime=${media.currentTime}, duration=${media.duration}`
);
</script>
</body>
</html>Result
currentTime=0 paused=true, muted=true, currentTime=2.5, duration=NaN
Save the example as an HTML file, open it in a browser, and check the developer console. The element has no source: `2.5` is the stored start position, not the result of seeking or playing. The element stays paused and `duration` remains `NaN`.
Related lessons
- JavaScript · Lesson 28 · Intermediate · Version: 3