Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

HTML Video and Audio Elements: Preload, Lazy Load, and Playback

How WHATWG defines video and audio loading, lazy preload precedence, autoplay rules, tracks, errors, and the media element API.

This page summarises the WHATWG HTML Living Standard rules for <video> and <audio>: when the browser fetches media, how loading and preload interact, autoplay and play() promises, lifecycle states, text tracks, and common author mistakes.

A <video> element plays video or movies, and can play audio files with captions. An <audio> element represents a sound or audio stream. Both share the same underlying HTMLMediaElement behaviour; the main difference is that audio has no visual playback area.

A video element is used for playing videos or movies, and audio files with captions.

— WHATWG, HTML Living Standard — Media

An audio element represents a sound or audio stream.

— WHATWG, HTML Living Standard — Media

video and audio: markup and fallback content

Section titled “video and audio: markup and fallback content”

Without a src attribute on the media element, you can list <source> children (with src, optional type and codecs) and <track> elements, then other transparent content. With src on the element, use zero or more <track> elements first.

Video-only attributes include poster (image URL while no video data is available), playsinline (hint to play inline rather than fullscreen), and width / height.

The children between the tags are fallback for legacy browsers, not an accessibility substitute for captions or transcripts.

In particular, this content is not intended to address accessibility concerns.

— WHATWG, HTML Living Standard — Media

For accessibility the spec points to: in-band captions, sign language, audio descriptions, WebVTT via <track>, chapter markers, and transcripts linked in prose near the element—not only inner fallback markup. See Edition for Web Developers — media for a shorter author-oriented summary of tracks.

Shared attributes: preload, loading, autoplay, and controls

Section titled “Shared attributes: preload, loading, autoplay, and controls”

Both support src, crossorigin, preload, autoplay, loop, muted, controls, and loading (lazy loading for out-of-viewport resources).

Attribute Role
preload Hint: auto (may fetch entire resource), metadata (metadata / first frames), none (do not expect to need data soon)
loading Lazy loading policy when outside the viewport; takes precedence over preload when in the Lazy state
autoplay UA may start playback when enough data is buffered, if eligible
muted Affects audible output and often autoplay policy in browsers
controls UA should expose playback UI when present (or scripting disabled)

The preload attribute is intended to provide a hint to the user agent about what the author thinks will lead to the best user experience. The attribute may be ignored altogether…

— WHATWG, HTML Living Standard — Media

When the loading attribute is in the Lazy state, it takes precedence over the preload attribute by deferring data fetching.

— WHATWG, HTML Living Standard — Media

loading="lazy" vs preload and the window load event

Section titled “loading="lazy" vs preload and the window load event”

While the media element’s delaying-the-load-event flag is set, parsing completion and the window load event can be delayed—similar to eager images. Spec examples show combinations such as default eager load (may delay load), in-viewport lazy (often loads after layout, does not delay load), below-the-fold lazy (loads when scrolled into view), autoplay plus lazy (playback and network deferred until visible), and preload="none" plus lazy (media may wait until play; poster may load when scrolled).

autoplay may override preload in the sense that both together is allowed; loading Lazy still defers fetching until lazy resumption runs.

Hidden audio (no controls) is not displayed, which affects lazy-loading behaviour:

audio elements without a controls attribute will not be displayed by the user agent, preventing them from lazy loading.

— WHATWG, HTML Living Standard — Media

If you rely on loading="lazy" for bandwidth, visible controls (or another display strategy the UA recognizes) matters for audio.

playsinline is a hint only; absence does not mean fullscreen by default:

The absence of the playsinline attribute does not imply that the video will display fullscreen by default.

— WHATWG, HTML Living Standard — Media

Resource selection: src, <source>, and srcObject

Section titled “Resource selection: src, <source>, and srcObject”

The media element loads from exactly one of three modes: srcObject (MediaStream, MediaSource, or Blob), the src attribute, or child <source> elements in tree order.

Changing a <source> element’s src or type after it is already in a playing <video> or <audio> has no effect. Use the media element’s src, srcObject, or load() after changing sources.

Dynamically modifying a source element’s src or type attribute when the element is already inserted in a video or audio element will have no effect. To change what is playing, just use the src attribute on the media element directly, possibly making use of the canPlayType() method to pick from amongst available resources.

— WHATWG, 4.8.2 The source element

On failed candidates the UA fires error on the <source> and tries the next; if none work, networkState becomes NETWORK_NO_SOURCE and MEDIA_ERR_SRC_NOT_SUPPORTED may apply.

canPlayType(type) returns "", "maybe", or "probably". Types that allow a codecs parameter should generally not return "probably" when codecs is omitted:

Generally, a user agent should never return “probably” for a type that allows the codecs parameter if that parameter is not present.

— WHATWG, HTML Living Standard — Media

Use explicit type='video/mp4; codecs="..."' on <source> so the UA can skip unsupported files without downloading them.

readyState, networkState, and playback API

Section titled “readyState, networkState, and playback API”
Constant Meaning (abbreviated)
HAVE_NOTHING (0) No information / no data at current position
HAVE_METADATA (1) Duration known; dimensions for video; no frame at current time
HAVE_CURRENT_DATA (2) Data for current position only
HAVE_FUTURE_DATA (3) Can play forward slightly; text tracks may be ready
HAVE_ENOUGH_DATA (4) Can play through at current rate

videoWidth / videoHeight are 0 when readyState === HAVE_NOTHING. Drawing video to a canvas depends on reaching at least HAVE_CURRENT_DATA for a real frame.

Events such as loadstart, progress, loadedmetadata, loadeddata, canplay, canplaythrough, waiting, playing, pause, ended, stalled, suspend, and error reflect transitions in the loading and playback algorithms in Media.

play() returns a Promise. Common rejections: NotAllowedError when autoplay or playback is blocked by policy, NotSupportedError when error is MEDIA_ERR_SRC_NOT_SUPPORTED.

User agents need not support autoplay; authors should prefer the autoplay attribute over script so users can override automatic playback (for example with assistive technology):

Authors are urged to use the autoplay attribute rather than using script to trigger automatic playback, as this allows the user to override the automatic playback when it is not desired, e.g. when using a screen reader.

— WHATWG, HTML Living Standard — Media

Autoplay eligibility in the spec includes constraints such as the autoplay attribute, lazy loading resumed or eager, sandbox without sandboxed automatic features, and permissions policy for "autoplay". A typical pattern for muted looping ads uses controls autoplay loop muted in the spec examples.

Calling load() resets lazy resumption, clears errors, detaches MediaSource, resets tracks, sets readyState to HAVE_NOTHING, and re-runs resource selection.

When media.error is set:

Code Constant Typical cause
1 MEDIA_ERR_ABORTED User or script aborted fetch
2 MEDIA_ERR_NETWORK Network failure after resource was usable
3 MEDIA_ERR_DECODE Decode failure
4 MEDIA_ERR_SRC_NOT_SUPPORTED No suitable resource or provider

The spec’s error-handling example uses onerror and e.target.error.code, then offers a download link—pattern worth copying in application code.

Text tracks, audio tracks, and cross-origin limits

Section titled “Text tracks, audio tracks, and cross-origin limits”

Kinds include subtitles, captions, descriptions, chapters, and metadata. Modes are disabled, hidden, and showing. addTextTrack(kind, label, language) creates a track in hidden mode and fires addtrack.

Cue timing should fire within about 20 ms of the media timeline during normal playback; timeupdate is throttled during playback.

Cross-origin media does not expose in-band subtitles to script (anti-intranet-leak):

Cross-origin videos do not expose their subtitles, since that would allow attacks such as hostile sites reading subtitles from confidential videos on a user’s intranet.

— WHATWG, HTML Living Standard — Media

When controls is present, the UA should expose play, pause, seek, volume, caption and track pickers, and fullscreen where applicable—and should implement UI via the same DOM APIs so events match script-driven control:

Where possible … user interface features exposed by the user agent must be implemented in terms of the DOM API described above, so that, e.g., all the same events fire.

— WHATWG, HTML Living Standard — Media

volume is 0.0–1.0; out-of-range sets throw IndexSizeError. Effective volume chains user override, system volume, and muted state.

For seekable ranges, UAs should be generous:

User agents should adopt a very liberal and optimistic view of what is seekable.

— WHATWG, HTML Living Standard — Media

fastSeek() may snap to keyframes; support varies by engine (noted in cluster excerpts for Chrome/Edge vs Firefox/Safari).

Common mistakes when using video and audio

Section titled “Common mistakes when using video and audio”
  • Treating fallback markup inside <video> as sufficient for screen reader users instead of captions, descriptions, or external transcripts.
  • Using <audio autoplay> without controls and expecting loading="lazy" to defer fetch.
  • Assuming preload="none" prevents all network activity while loading is not Lazy or the user starts playback.
  • Calling canPlayType('video/mp4') and expecting "probably" without codecs.
  • Ignoring play() promise rejections (NotAllowedError, NotSupportedError).
  • Updating <source src> after insert instead of changing the media element’s src or calling load().
  • Expecting scripted access to in-band captions on cross-origin media without CORS-safe same-origin delivery.
  • Autoplaying audible video without muted where policy blocks unmuted autoplay.
  1. Open the page in a browser, use developer tools Network to confirm when media and poster requests start relative to scroll and attributes (preload, loading, autoplay).
  2. In the console, read video.readyState, video.networkState, and video.error while loading and after failures; compare with definitions in HTML Living Standard — Media.
  3. Call video.canPlayType() with the same MIME strings you put on <source type="..."> and confirm the UA skips unsupported types without full downloads.
  4. Invoke video.play() and handle promise rejection (NotAllowedError / NotSupportedError); retest with muted and controls per Edition for Web Developers — media guidance.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you are asking about.