Skip to content

Commit 2328589

Browse files
committed
The HTML5 Sound Manager would unlock the Sound API on a touch event but only if the audio files were loaded in the first Scene, if they were loaded in a subsequent Scene the audio system would never unlock. It now unlocks only if there are audio files in the cache. Fix phaserjs#3311
1 parent 42d2cfd commit 2328589

3 files changed

Lines changed: 176 additions & 75 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* The RandomDataGenerator classes randomness has been improved thanks to the correct caching of a class property. Fix #3289 (thanks @migiyubi)
3838
* The RandomDataGenerator `sign` property had a method collision. Fix #3323 (thanks @vinerz and @samme)
3939
* In Arcade Physics World if you collided a group with itself it would call a missing method (`collideGroupVsSelf`), it now calls `collideGroupVsGroup` correctly (thanks @patrickgalbraith)
40+
* The HTML5 Sound Manager would unlock the Sound API on a touch event but only if the audio files were loaded in the first Scene, if they were loaded in a subsequent Scene the audio system would never unlock. It now unlocks only if there are audio files in the cache. Fix #3311 (thanks @chancezeus)
4041

4142
### Updates
4243

src/sound/html5/HTML5AudioSoundManager.js

Lines changed: 100 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
* @copyright 2018 Photon Storm Ltd.
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
6-
var Class = require('../../utils/Class');
6+
77
var BaseSoundManager = require('../BaseSoundManager');
8+
var Class = require('../../utils/Class');
89
var HTML5AudioSound = require('./HTML5AudioSound');
910

1011
/**
@@ -20,8 +21,12 @@ var HTML5AudioSound = require('./HTML5AudioSound');
2021
* @param {Phaser.Game} game - Reference to the current game instance.
2122
*/
2223
var HTML5AudioSoundManager = new Class({
24+
2325
Extends: BaseSoundManager,
24-
initialize: function HTML5AudioSoundManager (game)
26+
27+
initialize:
28+
29+
function HTML5AudioSoundManager (game)
2530
{
2631
/**
2732
* Flag indicating whether if there are no idle instances of HTML5 Audio tag,
@@ -77,6 +82,7 @@ var HTML5AudioSoundManager = new Class({
7782
* @since 3.0.0
7883
*/
7984
this.onBlurPausedSounds = [];
85+
8086
this.locked = 'ontouchstart' in window;
8187

8288
/**
@@ -115,6 +121,7 @@ var HTML5AudioSoundManager = new Class({
115121
* @since 3.0.0
116122
*/
117123
this._volume = 1;
124+
118125
BaseSoundManager.call(this, game);
119126
},
120127

@@ -132,7 +139,9 @@ var HTML5AudioSoundManager = new Class({
132139
add: function (key, config)
133140
{
134141
var sound = new HTML5AudioSound(this, key, config);
142+
135143
this.sounds.push(sound);
144+
136145
return sound;
137146
},
138147

@@ -147,21 +156,32 @@ var HTML5AudioSoundManager = new Class({
147156
unlock: function ()
148157
{
149158
var _this = this;
159+
150160
var moved = false;
161+
151162
var detectMove = function ()
152163
{
153164
moved = true;
154165
};
166+
155167
var unlock = function ()
156168
{
169+
if (!_this.game.cache.audio.entries.size)
170+
{
171+
return;
172+
}
173+
157174
if (moved)
158175
{
159176
moved = false;
160177
return;
161178
}
179+
162180
document.body.removeEventListener('touchmove', detectMove);
163181
document.body.removeEventListener('touchend', unlock);
182+
164183
var allTags = [];
184+
165185
_this.game.cache.audio.entries.each(function (key, tags)
166186
{
167187
for (var i = 0; i < tags.length; i++)
@@ -170,25 +190,30 @@ var HTML5AudioSoundManager = new Class({
170190
}
171191
return true;
172192
});
193+
173194
var lastTag = allTags[allTags.length - 1];
195+
174196
lastTag.oncanplaythrough = function ()
175197
{
176198
lastTag.oncanplaythrough = null;
177199
_this.unlocked = true;
178200
};
201+
179202
allTags.forEach(function (tag)
180203
{
181204
tag.load();
182205
});
183206
};
207+
184208
this.once('unlocked', function ()
185209
{
186-
_this.forEachActiveSound(function (sound)
210+
this.forEachActiveSound(function (sound)
187211
{
188212
sound.duration = sound.tags[0].duration;
189213
sound.totalDuration = sound.tags[0].duration;
190214
});
191-
_this.lockedActionsQueue.forEach(function (lockedAction)
215+
216+
this.lockedActionsQueue.forEach(function (lockedAction)
192217
{
193218
if (lockedAction.sound[lockedAction.prop].apply)
194219
{
@@ -199,9 +224,11 @@ var HTML5AudioSoundManager = new Class({
199224
lockedAction.sound[lockedAction.prop] = lockedAction.value;
200225
}
201226
});
202-
_this.lockedActionsQueue.length = 0;
203-
_this.lockedActionsQueue = null;
204-
});
227+
228+
this.lockedActionsQueue.length = 0;
229+
this.lockedActionsQueue = null;
230+
}, this);
231+
205232
document.body.addEventListener('touchmove', detectMove, false);
206233
document.body.addEventListener('touchend', unlock, false);
207234
},
@@ -240,6 +267,7 @@ var HTML5AudioSoundManager = new Class({
240267
{
241268
sound.onFocus();
242269
});
270+
243271
this.onBlurPausedSounds.length = 0;
244272
},
245273

@@ -253,6 +281,7 @@ var HTML5AudioSoundManager = new Class({
253281
destroy: function ()
254282
{
255283
BaseSoundManager.prototype.destroy.call(this);
284+
256285
this.onBlurPausedSounds.length = 0;
257286
this.onBlurPausedSounds = null;
258287
},
@@ -281,51 +310,79 @@ var HTML5AudioSoundManager = new Class({
281310
prop: prop,
282311
value: value
283312
});
313+
284314
return true;
285315
}
316+
286317
return false;
287-
}
288-
});
289-
Object.defineProperty(HTML5AudioSoundManager.prototype, 'mute', {
290-
get: function ()
291-
{
292-
return this._mute;
293318
},
294-
set: function (value)
295-
{
296-
this._mute = value;
297-
this.forEachActiveSound(function (sound)
319+
320+
/**
321+
* @event Phaser.Sound.HTML5AudioSoundManager#MuteEvent
322+
* @param {Phaser.Sound.HTML5AudioSoundManager} soundManager - Reference to the sound manager that emitted event.
323+
* @param {boolean} value - An updated value of Phaser.Sound.HTML5AudioSoundManager#mute property.
324+
*/
325+
326+
/**
327+
* @name Phaser.Sound.HTML5AudioSoundManager#mute
328+
* @type {boolean}
329+
* @fires Phaser.Sound.HTML5AudioSoundManager#MuteEvent
330+
* @since 3.0.0
331+
*/
332+
mute: {
333+
334+
get: function ()
298335
{
299-
sound.setMute();
300-
});
336+
return this._mute;
337+
},
338+
339+
set: function (value)
340+
{
341+
this._mute = value;
342+
343+
this.forEachActiveSound(function (sound)
344+
{
345+
sound.setMute();
346+
});
347+
348+
this.emit('mute', this, value);
349+
}
301350

302-
/**
303-
* @event Phaser.Sound.HTML5AudioSoundManager#mute
304-
* @param {Phaser.Sound.HTML5AudioSoundManager} soundManager - Reference to the sound manager that emitted event.
305-
* @param {boolean} value - An updated value of Phaser.Sound.HTML5AudioSoundManager#mute property.
306-
*/
307-
this.emit('mute', this, value);
308-
}
309-
});
310-
Object.defineProperty(HTML5AudioSoundManager.prototype, 'volume', {
311-
get: function ()
312-
{
313-
return this._volume;
314351
},
315-
set: function (value)
316-
{
317-
this._volume = value;
318-
this.forEachActiveSound(function (sound)
352+
353+
/**
354+
* @event Phaser.Sound.HTML5AudioSoundManager#VolumeEvent
355+
* @param {Phaser.Sound.HTML5AudioSoundManager} soundManager - Reference to the sound manager that emitted event.
356+
* @param {number} value - An updated value of Phaser.Sound.HTML5AudioSoundManager#volume property.
357+
*/
358+
359+
/**
360+
* @name Phaser.Sound.HTML5AudioSoundManager#volume
361+
* @type {number}
362+
* @fires Phaser.Sound.HTML5AudioSoundManager#VolumeEvent
363+
* @since 3.0.0
364+
*/
365+
volume: {
366+
367+
get: function ()
319368
{
320-
sound.setVolume();
321-
});
369+
return this._volume;
370+
},
371+
372+
set: function (value)
373+
{
374+
this._volume = value;
375+
376+
this.forEachActiveSound(function (sound)
377+
{
378+
sound.setVolume();
379+
});
380+
381+
this.emit('volume', this, value);
382+
}
322383

323-
/**
324-
* @event Phaser.Sound.HTML5AudioSoundManager#volume
325-
* @param {Phaser.Sound.HTML5AudioSoundManager} soundManager - Reference to the sound manager that emitted event.
326-
* @param {number} value - An updated value of Phaser.Sound.HTML5AudioSoundManager#volume property.
327-
*/
328-
this.emit('volume', this, value);
329384
}
385+
330386
});
387+
331388
module.exports = HTML5AudioSoundManager;

0 commit comments

Comments
 (0)