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
| name | type | optional | description |
|---|---|---|---|
| loader | Phaser.Loader.LoaderPlugin | No | A reference to the Loader that is responsible for this file. |
| key | string | Phaser.Types.Loader.FileTypes.AudioFileConfig | No | The key to use for this file, or a file configuration object. |
| urlConfig | Phaser.Types.Loader.FileTypes.AudioFileURLConfig | Yes | The absolute or relative URL to load this file from in a config object. |
| xhrSettings | Phaser.Types.Loader.XHRSettingsObject | Yes | Extra XHR Settings specifically for this file. |
| audioContext | AudioContext | Yes | The AudioContext this file will use to process itself. |
Scope: static
Extends
Source: src/loader/filetypes/AudioFile.js#L15
Since: 3.0.0
Inherited Methods
From Phaser.Loader.File:
- addToCache
- destroy
- hasCacheConflict
- load
- onBase64Load
- onError
- onLoad
- onProcessComplete
- onProcessError
- onProgress
- pendingDestroy
- resetXHR
- setLink
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:
| name | type | optional | description |
|---|---|---|---|
| loader | Phaser.Loader.LoaderPlugin | No | A reference to the Loader that is responsible for this file. |
| key | string | Phaser.Types.Loader.FileTypes.AudioFileConfig | No | The key to use for this file, or a file configuration object. |
| urls | string | Array.<string> | Phaser.Types.Loader.FileTypes.AudioFileURLConfig | Array.<Phaser.Types.Loader.FileTypes.AudioFileURLConfig> |
| config | any | Yes | An object containing an instances property for HTML5Audio. Defaults to 1. |
| xhrSettings | Phaser.Types.Loader.XHRSettingsObject | Yes | Extra 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:
| name | type | optional | description |
|---|---|---|---|
| game | Phaser.Game | No | A reference to the Phaser Game instance. |
| urls | string | Array.<string> | Phaser.Types.Loader.FileTypes.AudioFileURLConfig | Array.<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:
- base64
- bytesLoaded
- bytesTotal
- cache
- config
- crossOrigin
- data
- key
- linkFile
- loader
- multiFile
- percentComplete
- retryAttempts
- src
- state
- type
- url
- xhrLoader
- xhrSettings