Skip to content

Commit

Permalink
Restored the 3.55.2 sound system + fixes for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed Feb 4, 2022
1 parent f733f84 commit 4f0232e
Show file tree
Hide file tree
Showing 12 changed files with 499 additions and 1,389 deletions.
30 changes: 24 additions & 6 deletions src/loader/filetypes/AudioFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var IsPlainObject = require('../../utils/object/IsPlainObject');
* @param {(string|Phaser.Types.Loader.FileTypes.AudioFileConfig)} key - The key to use for this file, or a file configuration object.
* @param {any} [urlConfig] - The absolute or relative URL to load this file from in a config object.
* @param {Phaser.Types.Loader.XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
* @param {Phaser.Sound.WebAudioSoundManager} [soundManager] - The Web Audio Sound Manager.
* @param {AudioContext} [audioContext] - The AudioContext this file will use to process itself.
*/
var AudioFile = new Class({

Expand All @@ -39,14 +39,15 @@ var AudioFile = new Class({
initialize:

// URL is an object created by AudioFile.findAudioURL
function AudioFile (loader, key, urlConfig, xhrSettings, soundManager)
function AudioFile (loader, key, urlConfig, xhrSettings, audioContext)
{
if (IsPlainObject(key))
{
var config = key;

key = GetFastValue(config, 'key');
xhrSettings = GetFastValue(config, 'xhrSettings');
audioContext = GetFastValue(config, 'context', audioContext);
}

var fileConfig = {
Expand All @@ -57,7 +58,7 @@ var AudioFile = new Class({
key: key,
url: urlConfig.url,
xhrSettings: xhrSettings,
config: { soundManager: soundManager }
config: { context: audioContext }
};

File.call(this, loader, fileConfig);
Expand All @@ -74,9 +75,26 @@ var AudioFile = new Class({
{
this.state = CONST.FILE_PROCESSING;

this.config.soundManager.decodeAudio(this.key, this.xhrLoader.response);
var _this = this;

this.onProcessComplete();
// interesting read https://github.com/WebAudio/web-audio-api/issues/1305
this.config.context.decodeAudioData(this.xhrLoader.response,
function (audioBuffer)
{
_this.data = audioBuffer;

_this.onProcessComplete();
},
function (e)
{
// eslint-disable-next-line no-console
console.error('Error decoding audio: ' + _this.key + ' - ', e ? e.message : null);

_this.onProcessError();
}
);

this.config.context = null;
}

});
Expand Down Expand Up @@ -106,7 +124,7 @@ AudioFile.create = function (loader, key, urls, config, xhrSettings)

if (deviceAudio.webAudio && !audioConfig.disableWebAudio)
{
return new AudioFile(loader, key, urlConfig, xhrSettings, game.sound);
return new AudioFile(loader, key, urlConfig, xhrSettings, game.sound.context);
}
else
{
Expand Down
39 changes: 12 additions & 27 deletions src/loader/filetypes/HTML5AudioFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

var Class = require('../../utils/Class');
var CONST = require('../const');
var Events = require('../events');
var File = require('../File');
var GetFastValue = require('../../utils/object/GetFastValue');
Expand All @@ -17,7 +16,7 @@ var IsPlainObject = require('../../utils/object/IsPlainObject');
* A single Audio File suitable for loading by the Loader.
*
* 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.
*
* @class HTML5AudioFile
Expand Down Expand Up @@ -59,8 +58,7 @@ var HTML5AudioFile = new Class({
File.call(this, loader, fileConfig);

// New properties specific to this class
this.soundManager = loader.systems.sound;
this.locked = this.soundManager.locked;
this.locked = 'ontouchstart' in window;
this.loaded = false;
this.filesLoaded = 0;
this.filesTotal = 0;
Expand Down Expand Up @@ -109,8 +107,6 @@ var HTML5AudioFile = new Class({
* @method Phaser.Loader.FileTypes.HTML5AudioFile#onProgress
* @fires Phaser.Loader.Events#FILE_PROGRESS
* @since 3.0.0
*
* @param {ProgressEvent} event - The DOM ProgressEvent.
*/
onProgress: function (event)
{
Expand All @@ -127,11 +123,7 @@ var HTML5AudioFile = new Class({

if (this.filesLoaded === this.filesTotal)
{
var success = !(event.target && event.target.status !== 200);

this.state = CONST.FILE_LOADED;

this.loader.nextFile(this, success);
this.onLoad();
}
},

Expand All @@ -156,33 +148,26 @@ var HTML5AudioFile = new Class({
for (var i = 0; i < instances; i++)
{
var audio = new Audio();
var dataset = audio.dataset;

if (dataset === undefined)
if (!audio.dataset)
{
audio.dataset = dataset = {
name: '',
used: ''
};
audio.dataset = {};
}

dataset.name = this.key + '-' + i.toString();
dataset.used = 'false';
audio.dataset.name = this.key + ('0' + i).slice(-2);
audio.dataset.used = 'false';

if (this.locked)
{
dataset.locked = 'true';
// console.log('HTML5AudioFile:', dataset.name, 'locked');
audio.dataset.locked = 'true';
}
else
{
dataset.locked = 'false';
audio.dataset.locked = 'false';

audio.preload = 'auto';
audio.oncanplaythrough = this.onProgress.bind(this);
audio.onerror = this.onError.bind(this);

// console.log('HTML5AudioFile:', dataset.name, 'unlocked');
}

this.data.push(audio);
Expand All @@ -191,19 +176,19 @@ var HTML5AudioFile = new Class({
for (i = 0; i < this.data.length; i++)
{
audio = this.data[i];

audio.src = GetURL(this, this.loader.baseURL);

if (!this.locked)
{
audio.load();
// console.log('HTML5AudioFile:', dataset.name, 'load called');
}
}

if (this.locked)
{
this.loader.nextFile(this, true);
// This is super-dangerous but works. Race condition potential high.
// Is there another way?
setTimeout(this.onLoad.bind(this));
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/scene/SceneManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ var SceneManager = new Class({
*/
loadComplete: function (loader)
{
// TODO - Remove. This should *not* be handled here
// Try to unlock HTML5 sounds every time any loader completes
if (this.game.sound && this.game.sound.onBlurPausedSounds)
{
this.game.sound.unlock();
}

this.create(loader.scene);
},

Expand Down
Loading

0 comments on commit 4f0232e

Please sign in to comment.