Phaser API Documentation

audio(key, [urls], [config], [xhrSettings])


Adds an Audio or HTML5Audio file, or array of audio files, to the current load queue.

You can call this method from within your Scene's preload, along with any other files you wish to load:

function preload ()
{'title', [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ]);

The file is not loaded right away. It is added to a queue ready to be loaded either when the loader starts, or if it's already running, when the next free load slot becomes available. This happens automatically if you are calling this from within the Scene's preload method, or a related callback. Because the file is queued it means you cannot use the file immediately after calling this method, but must wait for the file to complete. The typical flow for a Phaser Scene is that you load assets in the Scene's preload method and then when the Scene's create method is called you are guaranteed that all of those assets are ready for use and have been loaded.

The key must be a unique String. It is used to add the file to the global Audio Cache upon a successful load. The key should be unique both in terms of files being loaded and files already present in the Audio Cache. Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file then remove it from the Audio Cache first, before loading a new one.

Instead of passing arguments you can pass a configuration object, such as:{
    key: 'title',
    url: [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ]

See the documentation for Phaser.Types.Loader.FileTypes.AudioFileConfig for more details.

The URLs can be relative or absolute. If the URLs are relative the Loader.baseURL and Loader.path values will be prepended to them.

Due to different browsers supporting different audio file types you should usually provide your audio files in a variety of formats. ogg, mp3 and m4a are the most common. If you provide an array of URLs then the Loader will determine which one file to load based on browser support.

If audio has been disabled in your game, either via the game config, or lack of support from the device, then no audio will be loaded.

Note: The ability to load this type of file will only be available if the Audio File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.


name type arguments description
key string | Phaser.Types.Loader.FileTypes.AudioFileConfig | Array.<Phaser.Types.Loader.FileTypes.AudioFileConfig>

The key to use for this file, or a file configuration object, or array of them.

urls string | Array.<string> <optional>

The absolute or relative URL to load the audio files from.

config any <optional>

An object containing an instances property for HTML5Audio. Defaults to 1.

xhrSettings Phaser.Types.Loader.XHRSettingsObject <optional>

An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.


The Loader instance.

Since: 3.0.0