Skip to content

Commit 0441e19

Browse files
committed
Updated jsdocs
1 parent ee2c061 commit 0441e19

5 files changed

Lines changed: 325 additions & 67 deletions

File tree

src/loader/filetypes/AudioFile.js

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,34 @@ var GetFastValue = require('../../utils/object/GetFastValue');
1212
var HTML5AudioFile = require('./HTML5AudioFile');
1313
var IsPlainObject = require('../../utils/object/IsPlainObject');
1414

15+
/**
16+
* @typedef {object} Phaser.Loader.FileTypes.AudioFileConfig
17+
*
18+
* @property {string} key - The key of the file. Must be unique within the Loader and Audio Cache.
19+
* @property {string} [urlConfig] - The absolute or relative URL to load the file from.
20+
* @property {XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
21+
* @property {AudioContext} [audioContext] - The AudioContext this file will use to process itself.
22+
*/
23+
1524
/**
1625
* @classdesc
17-
* [description]
26+
* A single Audio File suitable for loading by the Loader.
27+
*
28+
* These are created when you use the Phaser.Loader.LoaderPlugin#audio method and are not typically created directly.
29+
*
30+
* For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#audio.
1831
*
1932
* @class AudioFile
2033
* @extends Phaser.Loader.File
2134
* @memberOf Phaser.Loader.FileTypes
2235
* @constructor
2336
* @since 3.0.0
2437
*
25-
* @param {string} key - [description]
26-
* @param {string} url - [description]
27-
* @param {string} path - [description]
28-
* @param {XHRSettingsObject} [xhrSettings] - [description]
29-
* @param {AudioContext} [audioContext] - [description]
38+
* @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file.
39+
* @param {(string|Phaser.Loader.FileTypes.AudioFileConfig)} key - The key to use for this file, or a file configuration object.
40+
* @param {any} [urlConfig] - The absolute or relative URL to load this file from in a config object.
41+
* @param {XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
42+
* @param {AudioContext} [audioContext] - The AudioContext this file will use to process itself.
3043
*/
3144
var AudioFile = new Class({
3245

@@ -61,7 +74,8 @@ var AudioFile = new Class({
6174
},
6275

6376
/**
64-
* [description]
77+
* Called automatically by Loader.nextFile.
78+
* This method controls what extra work this File does with its loaded data.
6579
*
6680
* @method Phaser.Loader.FileTypes.AudioFile#onProcess
6781
* @since 3.0.0
@@ -160,22 +174,62 @@ AudioFile.getAudioURL = function (game, urls)
160174
};
161175

162176
/**
163-
* Adds an Audio file to the current load queue.
177+
* Adds an Audio or HTML5Audio file, or array of audio files, to the current load queue.
178+
*
179+
* You can call this method from within your Scene's `preload`, along with any other files you wish to load:
180+
*
181+
* ```javascript
182+
* function preload ()
183+
* {
184+
* this.load.audio('title', [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ]);
185+
* }
186+
* ```
187+
*
188+
* The file is **not** loaded right away. It is added to a queue ready to be loaded either when the loader starts,
189+
* or if it's already running, when the next free load slot becomes available. This happens automatically if you
190+
* are calling this from within the Scene's `preload` method, or a related callback. Because the file is queued
191+
* it means you cannot use the file immediately after calling this method, but must wait for the file to complete.
192+
* The typical flow for a Phaser Scene is that you load assets in the Scene's `preload` method and then when the
193+
* Scene's `create` method is called you are guaranteed that all of those assets are ready for use and have been
194+
* loaded.
195+
*
196+
* The key must be a unique String. It is used to add the file to the global Audio Cache upon a successful load.
197+
* The key should be unique both in terms of files being loaded and files already present in the Audio Cache.
198+
* Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file
199+
* then remove it from the Audio Cache first, before loading a new one.
200+
*
201+
* Instead of passing arguments you can pass a configuration object, such as:
202+
*
203+
* ```javascript
204+
* this.load.audio({
205+
* key: 'title',
206+
* url: [ 'music/Title.ogg', 'music/Title.mp3', 'music/Title.m4a' ]
207+
* });
208+
* ```
209+
*
210+
* See the documentation for `Phaser.Loader.FileTypes.AudioFileConfig` for more details.
211+
*
212+
* The URLs can be relative or absolute. If the URLs are relative the `Loader.baseURL` and `Loader.path` values will be prepended to them.
213+
*
214+
* Due to different browsers supporting different audio file types you should usually provide your audio files in a variety of formats.
215+
* 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
216+
* browser support.
164217
*
165-
* Note: This method will only be available if the Audio File type has been built into Phaser.
218+
* 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.
166219
*
167-
* The file is **not** loaded immediately after calling this method.
168-
* Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts.
220+
* Note: The ability to load this type of file will only be available if the Audio File type has been built into Phaser.
221+
* It is available in the default build but can be excluded from custom builds.
169222
*
170223
* @method Phaser.Loader.LoaderPlugin#audio
224+
* @fires Phaser.Loader.LoaderPlugin#addFileEvent
171225
* @since 3.0.0
172226
*
173-
* @param {string} key - [description]
174-
* @param {(string|string[])} urls - [description]
175-
* @param {object} config - [description]
176-
* @param {object} [xhrSettings] - [description]
227+
* @param {(string|Phaser.Loader.FileTypes.AudioFileConfig|Phaser.Loader.FileTypes.AudioFileConfig[])} key - The key to use for this file, or a file configuration object, or array of them.
228+
* @param {(string|string[])} [urls] - The absolute or relative URL to load the audio files from.
229+
* @param {any} [config] - An object containing an `instances` property for HTML5Audio. Defaults to 1.
230+
* @param {XHRSettingsObject} [xhrSettings] - An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
177231
*
178-
* @return {Phaser.Loader.LoaderPlugin} The Loader.
232+
* @return {Phaser.Loader.LoaderPlugin} The Loader instance.
179233
*/
180234
FileTypesManager.register('audio', function (key, urls, config, xhrSettings)
181235
{

src/loader/filetypes/AudioSpriteFile.js

Lines changed: 130 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,65 @@ var IsPlainObject = require('../../utils/object/IsPlainObject');
1212
var JSONFile = require('./JSONFile.js');
1313
var MultiFile = require('../MultiFile.js');
1414

15+
/**
16+
* @typedef {object} Phaser.Loader.FileTypes.AudioSpriteFileFileConfig
17+
*
18+
* @property {string} key - The key of the file. Must be unique within both the Loader and the Audio Cache.
19+
* @property {string} jsonURL - The absolute or relative URL to load the json file from. Or a well formed JSON object to use instead.
20+
* @property {XHRSettingsObject} [jsonXhrSettings] - Extra XHR Settings specifically for the json file.
21+
* @property {string} [audioURL] - The absolute or relative URL to load the audio file from.
22+
* @property {any} [audioConfig] - The audio configuration options.
23+
* @property {XHRSettingsObject} [audioXhrSettings] - Extra XHR Settings specifically for the audio file.
24+
*/
25+
1526
/**
1627
* @classdesc
17-
* An Audio Sprite JSON File.
28+
* An Audio Sprite File suitable for loading by the Loader.
29+
*
30+
* These are created when you use the Phaser.Loader.LoaderPlugin#audioSprite method and are not typically created directly.
31+
*
32+
* For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#audioSprite.
1833
*
1934
* @class AudioSpriteFile
2035
* @extends Phaser.Loader.MultiFile
2136
* @memberOf Phaser.Loader.FileTypes
2237
* @constructor
2338
* @since 3.7.0
2439
*
25-
* @param {string} key - The key of the file within the loader.
26-
* @param {string} textureURL - The url to load the texture file from.
27-
* @param {string} atlasURL - The url to load the atlas file from.
28-
* @param {string} path - The path of the file.
29-
* @param {XHRSettingsObject} [textureXhrSettings] - Optional texture file specific XHR settings.
30-
* @param {XHRSettingsObject} [atlasXhrSettings] - Optional atlas file specific XHR settings.
40+
* @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file.
41+
* @param {(string|Phaser.Loader.FileTypes.AudioSpriteFileConfig)} key - The key to use for this file, or a file configuration object.
42+
* @param {string} jsonURL - The absolute or relative URL to load the json file from. Or a well formed JSON object to use instead.
43+
* @param {string} [audioURL] - The absolute or relative URL to load the audio file from. If empty it will be obtained by parsing the JSON file.
44+
* @param {any} [audioConfig] - The audio configuration options.
45+
* @param {XHRSettingsObject} [audioXhrSettings] - An XHR Settings configuration object for the audio file. Used in replacement of the Loaders default XHR Settings.
46+
* @param {XHRSettingsObject} [jsonXhrSettings] - An XHR Settings configuration object for the json file. Used in replacement of the Loaders default XHR Settings.
3147
*/
3248
var AudioSpriteFile = new Class({
3349

3450
Extends: MultiFile,
3551

3652
initialize:
3753

38-
function AudioSpriteFile (loader, key, url, json, audioConfig, audioXhrSettings, jsonXhrSettings)
54+
function AudioSpriteFile (loader, key, jsonURL, audioURL, audioConfig, audioXhrSettings, jsonXhrSettings)
3955
{
4056
if (IsPlainObject(key))
4157
{
4258
var config = key;
4359

4460
key = GetFastValue(config, 'key');
45-
url = GetFastValue(config, 'url');
46-
json = GetFastValue(config, 'json');
47-
audioConfig = GetFastValue(config, 'config');
61+
jsonURL = GetFastValue(config, 'jsonURL');
62+
audioURL = GetFastValue(config, 'audioURL');
63+
audioConfig = GetFastValue(config, 'audioConfig');
4864
audioXhrSettings = GetFastValue(config, 'audioXhrSettings');
4965
jsonXhrSettings = GetFastValue(config, 'jsonXhrSettings');
5066
}
5167

5268
var data;
5369

5470
// No url? then we're going to do a json load and parse it from that
55-
if (!Array.isArray(url))
71+
if (!audioURL)
5672
{
57-
data = new JSONFile(loader, key, json, jsonXhrSettings);
73+
data = new JSONFile(loader, key, jsonURL, jsonXhrSettings);
5874

5975
MultiFile.call(this, loader, 'audiosprite', key, [ data ]);
6076

@@ -64,11 +80,11 @@ var AudioSpriteFile = new Class({
6480
}
6581
else
6682
{
67-
var audio = AudioFile.create(loader, key, url, audioConfig, audioXhrSettings);
83+
var audio = AudioFile.create(loader, key, audioURL, audioConfig, audioXhrSettings);
6884

6985
if (audio)
7086
{
71-
data = new JSONFile(loader, key, json, jsonXhrSettings);
87+
data = new JSONFile(loader, key, jsonURL, jsonXhrSettings);
7288

7389
MultiFile.call(this, loader, 'audiosprite', key, [ audio, data ]);
7490

@@ -80,7 +96,7 @@ var AudioSpriteFile = new Class({
8096
/**
8197
* Called by each File when it finishes loading.
8298
*
83-
* @method Phaser.Loader.MultiFile#onFileComplete
99+
* @method Phaser.Loader.AudioSpriteFile#onFileComplete
84100
* @since 3.7.0
85101
*
86102
* @param {Phaser.Loader.File} file - The File that has completed processing.
@@ -113,6 +129,12 @@ var AudioSpriteFile = new Class({
113129
}
114130
},
115131

132+
/**
133+
* Adds this file to its target cache upon successful loading and processing.
134+
*
135+
* @method Phaser.Loader.AudioSpriteFile#addToCache
136+
* @since 3.7.0
137+
*/
116138
addToCache: function ()
117139
{
118140
if (this.isReadyToProcess())
@@ -130,26 +152,106 @@ var AudioSpriteFile = new Class({
130152
});
131153

132154
/**
133-
* Adds an Audio Sprite file to the current load queue.
155+
* Adds a JSON based Audio Sprite, or array of audio sprites, to the current load queue.
156+
*
157+
* You can call this method from within your Scene's `preload`, along with any other files you wish to load:
158+
*
159+
* ```javascript
160+
* function preload ()
161+
* {
162+
* this.load.audioSprite('kyobi', 'kyobi.json', [
163+
* 'kyobi.ogg',
164+
* 'kyobi.mp3',
165+
* 'kyobi.m4a'
166+
* ]);
167+
* }
168+
* ```
169+
*
170+
* Audio Sprites are a combination of audio files and a JSON configuration.
171+
* The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite
172+
*
173+
* If the JSON file includes a 'resource' object then you can let Phaser parse it and load the audio
174+
* files automatically based on its content. To do this exclude the audio URLs from the load:
175+
*
176+
* ```javascript
177+
* function preload ()
178+
* {
179+
* this.load.audioSprite('kyobi', 'kyobi.json');
180+
* }
181+
* ```
182+
*
183+
* The file is **not** loaded right away. It is added to a queue ready to be loaded either when the loader starts,
184+
* or if it's already running, when the next free load slot becomes available. This happens automatically if you
185+
* are calling this from within the Scene's `preload` method, or a related callback. Because the file is queued
186+
* it means you cannot use the file immediately after calling this method, but must wait for the file to complete.
187+
* The typical flow for a Phaser Scene is that you load assets in the Scene's `preload` method and then when the
188+
* Scene's `create` method is called you are guaranteed that all of those assets are ready for use and have been
189+
* loaded.
190+
*
191+
* If you call this from outside of `preload` then you are responsible for starting the Loader afterwards and monitoring
192+
* its events to know when it's safe to use the asset. Please see the Phaser.Loader.LoaderPlugin class for more details.
193+
*
194+
* The key must be a unique String. It is used to add the file to the global Audio Cache upon a successful load.
195+
* The key should be unique both in terms of files being loaded and files already present in the Audio Cache.
196+
* Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file
197+
* then remove it from the Audio Cache first, before loading a new one.
198+
*
199+
* Instead of passing arguments you can pass a configuration object, such as:
200+
*
201+
* ```javascript
202+
* this.load.audioSprite({
203+
* key: 'kyobi',
204+
* jsonURL: 'audio/Kyobi.json',
205+
* audioURL: [
206+
* 'audio/Kyobi.ogg',
207+
* 'audio/Kyobi.mp3',
208+
* 'audio/Kyobi.m4a'
209+
* ]
210+
* });
211+
* ```
212+
*
213+
* See the documentation for `Phaser.Loader.FileTypes.AudioSpriteFileConfig` for more details.
214+
*
215+
* Instead of passing a URL for the audio JSON data you can also pass in a well formed JSON object instead.
216+
*
217+
* Once the audio has finished loading you can use it create an Audio Sprite by referencing its key:
218+
*
219+
* ```javascript
220+
* this.load.audioSprite('kyobi', 'kyobi.json');
221+
* // and later in your game ...
222+
* var music = this.sound.addAudioSprite('kyobi');
223+
* music.play('title');
224+
* ```
225+
*
226+
* If you have specified a prefix in the loader, via `Loader.setPrefix` then this value will be prepended to this files
227+
* key. For example, if the prefix was `MENU.` and the key was `Background` the final key will be `MENU.Background` and
228+
* this is what you would use to retrieve the image from the Texture Manager.
229+
*
230+
* The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it.
134231
*
135-
* Note: This method will only be available if the Audio Sprite File type has been built into Phaser.
232+
* Due to different browsers supporting different audio file types you should usually provide your audio files in a variety of formats.
233+
* 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
234+
* browser support.
136235
*
137-
* The file is **not** loaded immediately after calling this method.
138-
* Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts.
236+
* 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.
237+
*
238+
* Note: The ability to load this type of file will only be available if the Audio Sprite File type has been built into Phaser.
239+
* It is available in the default build but can be excluded from custom builds.
139240
*
140241
* @method Phaser.Loader.LoaderPlugin#audioSprite
242+
* @fires Phaser.Loader.LoaderPlugin#addFileEvent
141243
* @since 3.0.0
142244
*
143-
* @param {string} key - [description]
144-
* @param {object} json - [description]
145-
* @param {(string|string[])} urls - [description]
146-
* @param {object} config - [description]
147-
* @param {XHRSettingsObject} [audioXhrSettings] - Optional file specific XHR settings.
148-
* @param {XHRSettingsObject} [jsonXhrSettings] - Optional file specific XHR settings.
245+
* @param {(string|Phaser.Loader.FileTypes.AudioSpriteFileConfig|Phaser.Loader.FileTypes.AudioSpriteFileConfig[])} key - The key to use for this file, or a file configuration object, or an array of objects.
246+
* @param {string} jsonURL - The absolute or relative URL to load the json file from. Or a well formed JSON object to use instead.
247+
* @param {string} [audioURL] - The absolute or relative URL to load the audio file from. If empty it will be obtained by parsing the JSON file.
248+
* @param {any} [audioConfig] - The audio configuration options.
249+
* @param {XHRSettingsObject} [audioXhrSettings] - An XHR Settings configuration object for the audio file. Used in replacement of the Loaders default XHR Settings.
250+
* @param {XHRSettingsObject} [jsonXhrSettings] - An XHR Settings configuration object for the json file. Used in replacement of the Loaders default XHR Settings.
149251
*
150252
* @return {Phaser.Loader.LoaderPlugin} The Loader.
151253
*/
152-
FileTypesManager.register('audioSprite', function (key, json, urls, config, audioXhrSettings, jsonXhrSettings)
254+
FileTypesManager.register('audioSprite', function (key, jsonURL, audioURL, audioConfig, audioXhrSettings, jsonXhrSettings)
153255
{
154256
var game = this.systems.game;
155257
var audioConfig = game.config.audio;
@@ -181,7 +283,7 @@ FileTypesManager.register('audioSprite', function (key, json, urls, config, audi
181283
}
182284
else
183285
{
184-
multifile = new AudioSpriteFile(this, key, urls, json, config, audioXhrSettings, jsonXhrSettings);
286+
multifile = new AudioSpriteFile(this, key, jsonURL, audioURL, audioConfig, audioXhrSettings, jsonXhrSettings);
185287

186288
if (multifile.files)
187289
{

0 commit comments

Comments
 (0)