Phaser API Documentation

  Version: 
audioSprite(key, jsonURL, [audioURL], [audioConfig], [audioXhrSettings], [jsonXhrSettings])

Description:

Adds a JSON based Audio Sprite, or array of audio sprites, 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 ()
{
    this.load.audioSprite('kyobi', 'kyobi.json', [
        'kyobi.ogg',
        'kyobi.mp3',
        'kyobi.m4a'
    ]);
}

Audio Sprites are a combination of audio files and a JSON configuration. The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite

If the JSON file includes a 'resource' object then you can let Phaser parse it and load the audio files automatically based on its content. To do this exclude the audio URLs from the load:

function preload ()
{
    this.load.audioSprite('kyobi', 'kyobi.json');
}

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.

If you call this from outside of preload then you are responsible for starting the Loader afterwards and monitoring its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.

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:

this.load.audioSprite({
    key: 'kyobi',
    jsonURL: 'audio/Kyobi.json',
    audioURL: [
        'audio/Kyobi.ogg',
        'audio/Kyobi.mp3',
        'audio/Kyobi.m4a'
    ]
});

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

Instead of passing a URL for the audio JSON data you can also pass in a well formed JSON object instead.

Once the audio has finished loading you can use it create an Audio Sprite by referencing its key:

this.load.audioSprite('kyobi', 'kyobi.json');
// and later in your game ...
var music = this.sound.addAudioSprite('kyobi');
music.play('title');

If you have specified a prefix in the loader, via Loader.setPrefix then this value will be prepended to this files key. For example, if the prefix was MENU. and the key was Background the final key will be MENU.Background and this is what you would use to retrieve the image from the Texture Manager.

The URL can be relative or absolute. If the URL is relative the Loader.baseURL and Loader.path values will be prepended to it.

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 Sprite File type has been built into Phaser. It is available in the default build but can be excluded from custom builds.

Parameters:

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

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

jsonURL string

The absolute or relative URL to load the json file from. Or a well formed JSON object to use instead.

audioURL string | Array.<string> <optional>

The absolute or relative URL to load the audio file from. If empty it will be obtained by parsing the JSON file.

audioConfig any <optional>

The audio configuration options.

audioXhrSettings Phaser.Types.Loader.XHRSettingsObject <optional>

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

jsonXhrSettings Phaser.Types.Loader.XHRSettingsObject <optional>

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

Returns:
Description:

The Loader.

Since: 3.0.0