Skip to main content

preloadAudio()

This function is part of the @remotion/preload package.

This function preloads audio in the DOM so that when an HTML5 <audio> element is mounted, it can play immediately.

It only affects HTML5 audio elements such as <Html5Audio>, not <Audio> from @remotion/media.

warning

Using preloadAudio() is not recommended. Use premounting instead to give media time to load and initialize before it starts playing in the Player or Studio.

Usage​

import {preloadAudio} from '@remotion/preload';

const unpreload = preloadAudio('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3');

// If you want to un-preload the audio later
unpreload();

How it works​

  • On Firefox, it appends a <link rel="preload" as="audio"> tag in the head element of the document.
  • In other browsers, it appends an <audio preload="auto"> tag in the body element of the document. On iOS Safari, preload="metadata" is used instead.

Handle redirects​

preloadAudio() resolves redirects automatically. It first fetches the URL to determine the final URL after any redirects, and then preloads the final URL.

This requires the resource to support CORS. If the URL cannot be fetched, for example because the resource does not support CORS or the request times out, nothing gets preloaded and the error is logged to the console.

To obtain the final URL yourself, use resolveRedirect().

Compatibility​

BrowsersEnvironments
Chrome
Firefox
Safari

This function preloads HTML5 audio elements. It does not preload the assets used by @remotion/media for client-side rendering.

See also​