--- url: "https://web.dev/articles/fast-playback-with-preload" titulek: "Fast playback with audio and video preload | Articles | web.dev" h1: "Fast playback with audio and video preload Stay organized with collections Save and categorize content based on your preferences." popis: "Faster playback start means more people watching your video or listening to your audio. That's a known fact. In this article I'll explore techniques you can use to accelerate your media playback by actively preloading resources depending on your use case." jazyk: "en" kanonicka: "https://web.dev/articles/fast-playback-with-preload" drobecky: ["Articles", "Fast playback with audio and video preload"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:35:48" znaku: 17803 slov: 2418 kodovani: "iso-8859-2" sha256: "3907e9fa6718639f1a1d312278001d62179f9d1ad3816772c9aa5605ebbf21b4" --- Fast playback with audio and video preload Stay organized with collections Save and categorize content based on your preferences. How to accelerate your media playback by actively preloading resources. François Beaufort Faster playback start means more people watching your video or listening to your audio. [That's a known fact](https://www.digitaltrends.com/web/buffer-rage/). In this article I'll explore techniques you can use to accelerate your audio and video playback by actively preloading resources depending on your use case. Credits: copyright Blender Foundation | [www.blender.org](http://www.blender.org). I'll describe three methods of preloading media files, starting with their pros and cons. It's great... But... Video preload attribute Simple to use for a unique file hosted on a web server. Browsers may completely ignore the attribute. Resource fetching starts when the HTML document has been completely loaded and parsed. Media Source Extensions (MSE) ignore the `preload` attribute on media elements because the app is responsible for providing media to MSE. Link preload Forces the browser to make a request for a video resource without blocking the document's `onload` event. HTTP Range requests are not compatible. Compatible with MSE and file segments. Should be used only for small media files (<5 MB) when fetching full resources. Manual buffering Full control Complex error handling is the website's responsibility. Video preload attribute If the video source is a unique file hosted on a web server, you may want to use the video `preload` attribute to provide a hint to the browser as to [how much information or content to preload](/video-and-source-tags#preload). This means [Media Source Extensions (MSE)](https://developer.mozilla.org/docs/Web/API/Media_Source_Extensions_API) is not compatible with `preload`. Resource fetching will start only when the initial HTML document has been completely loaded and parsed (e.g. the `DOMContentLoaded` event has fired) while the very different `load` event will be fired when resource has actually been fetched. Setting the `preload` attribute to `metadata` indicates that the user is not expected to need the video, but that fetching its metadata (dimensions, track list, duration, and so on) is desirable. Note that starting in [Chrome 64](https://developers.google.com/web/updates/2017/12/chrome-63-64-media-updates#media-preload-defaults-metadata), the default value for `preload` is `metadata`. (It was `auto` previously). ` ` Setting the `preload` attribute to `auto` indicates that the browser may cache enough data that complete playback is possible without requiring a stop for further buffering. ` ` There are some caveats though. As this is just a hint, the browser may completely ignore the `preload` attribute. At the time of writing, here are some rules applied in Chrome: When [Data Saver](https://support.google.com/chrome/answer/2392284) is enabled, Chrome forces the `preload` value to `none`. In Android 4.3, Chrome forces the `preload` value to `none` due to an [Android Bug](https://bugs.chromium.org/p/chromium/issues/detail?id=612909). On a cellular connection (2G, 3G, and 4G), Chrome forces the `preload` value to `metadata`. Tips If your website contains many video resources on the same domain, I would recommend you set the `preload` value to `metadata` or define the `poster` attribute and set `preload` to `none`. That way, you would avoid hitting the maximum number of HTTP connections to the same domain (6 according to the HTTP 1.1 spec) which can hang loading of resources. Note that this may also improve page speed if videos aren't part of your core user experience. Link preload As [covered](https://developer.chrome.com/blog/link-rel-preload/) in other [articles](https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/), [link preload](https://w3c.github.io/preload/) is a declarative fetch that allows you to force the browser to make a request for a resource without blocking the `load` event and while the page is downloading. Resources loaded via `` are stored locally in the browser, and are effectively inert until they're explicitly referenced in the DOM, JavaScript, or CSS. Preload is different from prefetch in that it focuses on current navigation and fetches resources with priority based on their type (script, style, font, video, audio, etc.). It should be used to warm up the browser cache for current sessions. Preload full video Here's how to preload a full video on your website so that when your JavaScript asks to fetch video content, it is read from cache as the resource may have already been cached by the browser. If the preload request hasn't finished yet, a regular network fetch will happen. ` ` Because the preloaded resource is going to be consumed by a video element in the example, the `as` preload link value is `video`. If it were an audio element, it would be `as="audio"`. Preload the first segment The example below shows how to preload the first segment of a video with `` and use it with Media Source Extensions. If you're not familiar with the MSE JavaScript API, see [MSE basics](https://developer.mozilla.org/docs/Web/API/Media_Source_Extensions_API). For the sake of simplicity, let's assume the entire video has been split into smaller files like `file_1.webm`, `file_2.webm`, `file_3.webm`, etc. ` ` Support You can detect support of various `as` types for `` with the snippets below: `function preloadFullVideoSupported() { const link = document.createElement('link'); link.as = 'video'; return (link.as === 'video'); } function preloadFirstSegmentSupported() { const link = document.createElement('link'); link.as = 'fetch'; return (link.as === 'fetch'); }` Manual buffering Before we dive into the [Cache API](https://developer.mozilla.org/docs/Web/API/Cache) and service workers, let's see how to manually buffer a video with MSE. The example below assumes that your web server supports HTTP [`Range`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Range) requests but this would be pretty similar with file segments. Note that some middleware libraries such as [Google's Shaka Player](https://github.com/google/shaka-player), [JW Player](https://developer.jwplayer.com/), and [Video.js](http://videojs.com/) are built to handle this for you. ` ` Considerations As you're now in control of the entire media buffering experience, I suggest you consider the device's battery level, the "Data-Saver Mode" user preference and network information when thinking about preloading. Battery awareness Take into account the battery level of users' devices before thinking about preloading a video. This will preserve battery life when the power level is low. Disable preload or at least preload a lower resolution video when the device is running out of battery. `if ('getBattery' in navigator) { navigator.getBattery() .then(battery => { // If battery is charging or battery level is high enough if (battery.charging || battery.level > 0.15) { // TODO: Preload the first segment of a video. } }); }` Detect "Data-Saver" Use the `Save-Data` client hint request header to deliver fast and light applications to users who have opted-in to "data savings" mode in their browser. By identifying this request header, your application can customize and deliver an optimized user experience to cost- and performance-constrained users. See [Delivering Fast and Light Applications with Save-Data](/optimizing-content-efficiency-save-data) to learn more. Smart loading based on network information You may want to check `navigator.connection.type` prior to preloading. When it's set to `cellular`, you could prevent preloading and advise users that their mobile network operator might be charging for the bandwidth, and only start automatic playback of previously cached content. `if ('connection' in navigator) { if (navigator.connection.type == 'cellular') { // TODO: Prompt user before preloading video } else { // TODO: Preload the first segment of a video. } }` Check out the [Network Information sample](https://googlechrome.github.io/samples/network-information/) to learn how to react to network changes as well. Pre-cache multiple first segments Now what if I want to speculatively pre-load some media content without knowing which piece of media the user will eventually pick? If the user is on a web page that contains 10 videos, we probably have enough memory to fetch one segment file from each but we should definitely not create 10 hidden `