Skip to main content
Version: Phaser v4.0.0

AudioFile

A single Audio File suitable for loading by the Loader via the Web Audio API.

This file type loads audio data as an ArrayBuffer over XHR and then decodes it into an AudioBuffer using the Web Audio API's decodeAudioData method. The resulting AudioBuffer is stored in the Phaser Audio Cache, ready for use by the Sound Manager.

If the device does not support the Web Audio API, or if Web Audio has been disabled in the game configuration, an HTML5AudioFile will be created instead. You do not need to choose between them manually — use Phaser.Loader.LoaderPlugin#audio and Phaser will select the correct file type automatically based on device capabilities.

These are created when you use the Phaser.Loader.LoaderPlugin#audio method and are not typically created directly.

For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#audio.

Constructor

new AudioFile(loader, key, [urlConfig], [xhrSettings], [audioContext])

Parameters

nametypeoptionaldescription
loaderPhaser.Loader.LoaderPluginNoA reference to the Loader that is responsible for this file.
keystring | Phaser.Types.Loader.FileTypes.AudioFileConfigNoThe key to use for this file, or a file configuration object.
urlConfigPhaser.Types.Loader.FileTypes.AudioFileURLConfigYesThe absolute or relative URL to load this file from in a config object.
xhrSettingsPhaser.Types.Loader.XHRSettingsObjectYesExtra XHR Settings specifically for this file.
audioContextAudioContextYesThe AudioContext this file will use to process itself.

Scope: static

Extends

Phaser.Loader.File

Source: src/loader/filetypes/AudioFile.js#L15
Since: 3.0.0

Inherited Methods

From Phaser.Loader.File:


Public Methods

create

<static> create(loader, key, [urls], [config], [xhrSettings])

Description:

Static factory method that creates the correct type of audio file for the current device.

Inspects the game's audio configuration and device capabilities to decide whether to return a Web Audio API-based AudioFile or a fallback HTML5AudioFile. It first calls AudioFile.getAudioURL to find a suitable URL from the provided list that the browser can play. If no compatible URL is found, a warning is logged and null is returned.

Parameters:

nametypeoptionaldescription
loaderPhaser.Loader.LoaderPluginNoA reference to the Loader that is responsible for this file.
keystring | Phaser.Types.Loader.FileTypes.AudioFileConfigNoThe key to use for this file, or a file configuration object.
urlsstring | Array.<string>Phaser.Types.Loader.FileTypes.AudioFileURLConfigArray.<Phaser.Types.Loader.FileTypes.AudioFileURLConfig>
configanyYesAn object containing an instances property for HTML5Audio. Defaults to 1.
xhrSettingsPhaser.Types.Loader.XHRSettingsObjectYesExtra XHR Settings specifically for this file.

Returns: Phaser.Loader.FileTypes.AudioFile, Phaser.Loader.FileTypes.HTML5AudioFile - The created audio file instance, or null if no supported URL was found.

Source: src/loader/filetypes/AudioFile.js#L115
Since: 3.0.0


getAudioURL

<static> getAudioURL(game, urls)

Description:

Takes an array of audio URLs and returns a URL config object for the first entry that the current device is capable of playing, based on the audio format indicated by the file extension or an explicit type property on the URL config object.

Blob and data URIs are always accepted and returned immediately regardless of format. For regular URLs, the extension is extracted from the filename and checked against game.device.audio. If no supported URL is found, null is returned.

Parameters:

nametypeoptionaldescription
gamePhaser.GameNoA reference to the Phaser Game instance.
urlsstring | Array.<string>Phaser.Types.Loader.FileTypes.AudioFileURLConfigArray.<Phaser.Types.Loader.FileTypes.AudioFileURLConfig>

Returns: Phaser.Types.Loader.FileTypes.AudioFileURLConfig - A URL config object with url and type properties for the first supported audio URL, or null if none are supported.

Source: src/loader/filetypes/AudioFile.js#L169
Since: 3.0.0


onProcess

<instance> onProcess()

Description:

Called automatically by Loader.nextFile. This method decodes the raw audio ArrayBuffer loaded via XHR using the Web Audio API's decodeAudioData method. On success, the resulting AudioBuffer is stored in this.data and onProcessComplete is called to advance the load queue. On failure, an error is logged to the console and onProcessError is called. The AudioContext reference is released after decoding begins to avoid retaining it unnecessarily.

Overrides: Phaser.Loader.File#onProcess

Source: src/loader/filetypes/AudioFile.js#L76
Since: 3.0.0


Inherited Members

From Phaser.Loader.File: