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”What belongs inside <video> or <audio>
Section titled “What belongs inside <video> or <audio>”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”Attributes common to video and audio
Section titled “Attributes common to video and audio”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.
audio without controls and lazy loading
Section titled “audio without controls and lazy loading”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 on video
Section titled “playsinline on video”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
sourceelement
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() and MIME types
Section titled “canPlayType() and MIME types”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”readyState and when data is usable
Section titled “readyState and when data is usable”| 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(), pause(), and autoplay eligibility
Section titled “play(), pause(), and autoplay eligibility”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.
MediaError codes and handling failures
Section titled “MediaError codes and handling failures”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”Text tracks and addTextTrack()
Section titled “Text tracks and addTextTrack()”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
Built-in controls and volume
Section titled “Built-in controls and volume”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.
Seeking and fastSeek
Section titled “Seeking and fastSeek”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>withoutcontrolsand expectingloading="lazy"to defer fetch. - Assuming
preload="none"prevents all network activity whileloadingis not Lazy or the user starts playback. - Calling
canPlayType('video/mp4')and expecting"probably"withoutcodecs. - Ignoring
play()promise rejections (NotAllowedError,NotSupportedError). - Updating
<source src>after insert instead of changing the media element’ssrcor callingload(). - Expecting scripted access to in-band captions on cross-origin media without CORS-safe same-origin delivery.
- Autoplaying audible video without
mutedwhere policy blocks unmuted autoplay.
How to check this on your site
Section titled “How to check this on your site”- Open the page in a browser, use developer tools Network to confirm when media and
posterrequests start relative to scroll and attributes (preload,loading,autoplay). - In the console, read
video.readyState,video.networkState, andvideo.errorwhile loading and after failures; compare with definitions in HTML Living Standard — Media. - Call
video.canPlayType()with the same MIME strings you put on<source type="...">and confirm the UA skips unsupported types without full downloads. - Invoke
video.play()and handle promise rejection (NotAllowedError/NotSupportedError); retest withmutedandcontrolsper Edition for Web Developers — media guidance. - You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”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.