Skip to content

Commit b63f687

Browse files
committed
Loader: better audio selection
- Fixed bug where `.` in query portion of URI could throw off auto-detection - Allow `Loader#audio` to accept `{uri..,type..}` objects to blobs (and data) URIs can also be used to auto-format detection / fallbacks.
1 parent 86dc438 commit b63f687

1 file changed

Lines changed: 30 additions & 18 deletions

File tree

src/loader/Loader.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -718,15 +718,22 @@ Phaser.Loader.prototype = {
718718
*
719719
* @method Phaser.Loader#audio
720720
* @param {string} key - Unique asset key of the audio file.
721-
* @param {string[]|string} urls - An array containing the URLs of the audio files, i.e.: [ 'jump.mp3', 'jump.ogg', 'jump.m4a' ] or a single string containing just one URL.
722-
* BLOB urls are supported, but Phaser will not validate the audio file's type in this case; the program must first ensure the BLOB url is playable.
723-
* @param {boolean} autoDecode - When using Web Audio the audio files can either be decoded at load time or run-time. They can't be played until they are decoded, but this let's you control when that happens. Decoding is a non-blocking async process.
721+
* @param {string|string[]|object[]} urls - Either a single string or an array of URIs or pairs of `{uri: .., type: ..}`.
722+
* If an array is specified then the first URI (or URI + mime pair) that is device-compatible will be selected.
723+
* For example: `"jump.mp3"`, `['jump.mp3', 'jump.ogg', 'jump.m4a']`, or `[{uri: "data:<opus_resource>", type: 'opus'}, 'fallback.mp3']`.
724+
* BLOB and DATA URIs can be used but only support automatic detection when used in the pair form; otherwise the format must be manually checked before adding the resource.
725+
* @param {boolean} autoDecode - When using Web Audio the audio files can either be decoded at load time or run-time.
726+
* Audio files can't be played until they are decoded and, if specified, this enables immediate decoding. Decoding is a non-blocking async process.
724727
* @return {Phaser.Loader} This Loader instance.
725728
*/
726729
audio: function (key, urls, autoDecode) {
727730

728731
if (typeof autoDecode === 'undefined') { autoDecode = true; }
729732

733+
if (typeof urls === 'string') {
734+
urls = [urls];
735+
}
736+
730737
this.addToFileList('audio', key, urls, { buffer: null, autoDecode: autoDecode });
731738

732739
return this;
@@ -1176,9 +1183,7 @@ Phaser.Loader.prototype = {
11761183
// When true further non-pack file downloads are suppressed
11771184
var syncblock = false;
11781185

1179-
var inflightLimit = this.enableParallel
1180-
? Phaser.Math.clamp(this.maxParallelDownloads, 1, 12)
1181-
: 1;
1186+
var inflightLimit = this.enableParallel ? Phaser.Math.clamp(this.maxParallelDownloads, 1, 12) : 1;
11821187

11831188
for (var i = this._processingHead; i < this._fileList.length; i++)
11841189
{
@@ -1729,32 +1734,39 @@ Phaser.Loader.prototype = {
17291734
*
17301735
* @method Phaser.Loader#getAudioURL
17311736
* @private
1732-
* @param {string[]|string} urls - Either an array of audio file URLs or a string containing a single URL path.
1737+
* @param {object[]||string[]} urls - See {@link #audio} for format.
17331738
* @return {string} The URL to try and fetch; or null.
17341739
*/
17351740
getAudioURL: function (urls) {
17361741

1737-
if (typeof urls === 'string') { urls = [urls]; }
1738-
17391742
for (var i = 0; i < urls.length; i++)
17401743
{
17411744
var url = urls[i];
1745+
var audioType;
17421746

1743-
// It is assumed that a direct-data URI can be played.
1744-
if (url.indexOf("blob:") === 0 || url.indexOf("data:") === 0)
1747+
if (url.uri) // {uri: .., type: ..} pair
17451748
{
1746-
return url;
1749+
url = url.uri;
1750+
audioType = url.type;
17471751
}
1752+
else
1753+
{
1754+
// Assume direct-data URI can be played if not in a paired form; select immediately
1755+
if (url.indexOf("blob:") === 0 || url.indexOf("data:") === 0)
1756+
{
1757+
return url;
1758+
}
17481759

1749-
var extension = url.toLowerCase();
1750-
extension = extension.substr((Math.max(0, extension.lastIndexOf(".")) || Infinity) + 1);
1760+
if (url.indexOf("?") >= 0) // Remove query from URL
1761+
{
1762+
url = url.substr(0, url.indexOf("?"));
1763+
}
17511764

1752-
if (extension.indexOf("?") >= 0)
1753-
{
1754-
extension = extension.substr(0, extension.indexOf("?"));
1765+
var extension = url.substr((Math.max(0, extension.lastIndexOf(".")) || Infinity) + 1);
1766+
audioType = extension.toLowerCase();
17551767
}
17561768

1757-
if (this.game.device.canPlayAudio(extension))
1769+
if (this.game.device.canPlayAudio(audioType))
17581770
{
17591771
return urls[i];
17601772
}

0 commit comments

Comments
 (0)