Skip to content

Commit 02be81c

Browse files
committed
Merge branch 'master' of https://github.com/photonstorm/phaser
2 parents e37b183 + 8b24731 commit 02be81c

7 files changed

Lines changed: 66 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Fixed issue in HTMLAudioSound where `mute` would get into a recursive loop.
1717
* Every RenderTexture would draw the same content due to a mis-use of the CanvasPool (this also impacted TileSprites). Fix #3555 (thanks @kuoruan)
1818
* Group.add and Group.addMultiple now respect the Group.maxSize property, stopping you from over-populating a Group (thanks @samme)
19+
* When using HTML5 Audio, sound manager now tries to unlock audio after every scene loads, instead of only after first one. Fix #3309 (thanks @pavle-goloskokovic)
1920

2021
### Updates
2122

src/loader/filetypes/AudioFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ AudioFile.create = function (loader, key, urls, config, xhrSettings)
122122
}
123123
else
124124
{
125-
return new HTML5AudioFile(key, url, loader.path, config, game.sound.locked);
125+
return new HTML5AudioFile(key, url, loader.path, config);
126126
}
127127
};
128128

src/loader/filetypes/HTML5AudioFile.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ var GetURL = require('../GetURL');
2323
* @param {string} url - [description]
2424
* @param {string} path - [description]
2525
* @param {XHRSettingsObject} config - [description]
26-
* @param {boolean} locked - [description]
2726
*/
2827
var HTML5AudioFile = new Class({
2928

3029
Extends: File,
3130

3231
initialize:
3332

34-
function HTML5AudioFile (key, url, path, config, locked)
33+
function HTML5AudioFile (key, url, path, config)
3534
{
36-
this.locked = locked;
35+
this.locked = 'ontouchstart' in window;
3736

3837
this.loaded = false;
3938

@@ -110,8 +109,14 @@ var HTML5AudioFile = new Class({
110109
audio.dataset.name = this.key + ('0' + i).slice(-2); // Useful for debugging
111110
audio.dataset.used = 'false';
112111

113-
if (!this.locked)
112+
if (this.locked)
113+
{
114+
audio.dataset.locked = 'true';
115+
}
116+
else
114117
{
118+
audio.dataset.locked = 'false';
119+
115120
audio.preload = 'auto';
116121
audio.oncanplaythrough = this.onProgress.bind(this);
117122
audio.onerror = this.onError.bind(this);

src/scene/SceneManager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,12 @@ var SceneManager = new Class({
499499
{
500500
var scene = loader.scene;
501501

502+
// Try to unlock HTML5 sounds every time any loader completes
503+
if (this.game.sound.onBlurPausedSounds)
504+
{
505+
this.game.sound.unlock();
506+
}
507+
502508
this.create(scene);
503509
},
504510

src/sound/BaseSoundManager.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ var NOOP = require('../utils/NOOP');
3131
* @classdesc
3232
* The sound manager is responsible for playing back audio via Web Audio API or HTML Audio tag as fallback.
3333
* The audio file type and the encoding of those files are extremely important.
34-
*
34+
*
3535
* Not all browsers can play all audio formats.
36-
*
36+
*
3737
* There is a good guide to what's supported [here](https://developer.mozilla.org/en-US/Apps/Fundamentals/Audio_and_video_delivery/Cross-browser_audio_basics#Audio_Codec_Support).
3838
*
3939
* @class BaseSoundManager
@@ -169,11 +169,6 @@ var BaseSoundManager = new Class({
169169
* @since 3.0.0
170170
*/
171171
this.unlocked = false;
172-
173-
if (this.locked)
174-
{
175-
this.unlock();
176-
}
177172
},
178173

179174
/**
@@ -537,7 +532,7 @@ var BaseSoundManager = new Class({
537532

538533
/**
539534
* Sets the global playback rate at which all the sounds will be played.
540-
*
535+
*
541536
* For example, a value of 1.0 plays the audio at full speed, 0.5 plays the audio at half speed
542537
* and 2.0 doubles the audios playback speed.
543538
*

src/sound/html5/HTML5AudioSoundManager.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ var HTML5AudioSoundManager = new Class({
9696
* @private
9797
* @since 3.0.0
9898
*/
99-
this.lockedActionsQueue = this.locked ? [] : null;
99+
this.lockedActionsQueue = null;
100100

101101
/**
102102
* Property that actually holds the value of global mute
@@ -154,6 +154,17 @@ var HTML5AudioSoundManager = new Class({
154154
*/
155155
unlock: function ()
156156
{
157+
this.locked = 'ontouchstart' in window;
158+
159+
if(this.locked)
160+
{
161+
this.lockedActionsQueue = [];
162+
}
163+
else
164+
{
165+
return;
166+
}
167+
157168
var _this = this;
158169

159170
var moved = false;
@@ -165,11 +176,6 @@ var HTML5AudioSoundManager = new Class({
165176

166177
var unlock = function ()
167178
{
168-
if (!_this.game.cache.audio.entries.size)
169-
{
170-
return;
171-
}
172-
173179
if (moved)
174180
{
175181
moved = false;
@@ -179,26 +185,43 @@ var HTML5AudioSoundManager = new Class({
179185
document.body.removeEventListener('touchmove', detectMove);
180186
document.body.removeEventListener('touchend', unlock);
181187

182-
var allTags = [];
188+
var lockedTags = [];
183189

184190
_this.game.cache.audio.entries.each(function (key, tags)
185191
{
186192
for (var i = 0; i < tags.length; i++)
187193
{
188-
allTags.push(tags[i]);
194+
var tag = tags[i];
195+
196+
if (tag.dataset.locked === 'true')
197+
{
198+
lockedTags.push(tag);
199+
}
189200
}
201+
190202
return true;
191203
});
192204

193-
var lastTag = allTags[allTags.length - 1];
205+
if (lockedTags.length === 0)
206+
{
207+
return;
208+
}
209+
210+
var lastTag = lockedTags[lockedTags.length - 1];
194211

195212
lastTag.oncanplaythrough = function ()
196213
{
197214
lastTag.oncanplaythrough = null;
215+
216+
lockedTags.forEach(function (tag)
217+
{
218+
tag.dataset.locked = 'false';
219+
});
220+
198221
_this.unlocked = true;
199222
};
200223

201-
allTags.forEach(function (tag)
224+
lockedTags.forEach(function (tag)
202225
{
203226
tag.load();
204227
});
@@ -208,7 +231,11 @@ var HTML5AudioSoundManager = new Class({
208231
{
209232
this.forEachActiveSound(function (sound)
210233
{
211-
sound.duration = sound.tags[0].duration;
234+
if(sound.currentMarker === null && sound.duration === 0)
235+
{
236+
sound.duration = sound.tags[0].duration;
237+
}
238+
212239
sound.totalDuration = sound.tags[0].duration;
213240
});
214241

@@ -226,6 +253,7 @@ var HTML5AudioSoundManager = new Class({
226253

227254
this.lockedActionsQueue.length = 0;
228255
this.lockedActionsQueue = null;
256+
229257
}, this);
230258

231259
document.body.addEventListener('touchmove', detectMove, false);
@@ -302,7 +330,7 @@ var HTML5AudioSoundManager = new Class({
302330
*/
303331
isLocked: function (sound, prop, value)
304332
{
305-
if (this.locked)
333+
if (sound.tags[0].dataset.locked === 'true')
306334
{
307335
this.lockedActionsQueue.push({
308336
sound: sound,

src/sound/webaudio/WebAudioSoundManager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ var WebAudioSoundManager = new Class({
7676
this.locked = this.context.state === 'suspended' && 'ontouchstart' in window;
7777

7878
BaseSoundManager.call(this, game);
79+
80+
if (this.locked)
81+
{
82+
this.unlock();
83+
}
7984
},
8085

8186
/**
@@ -145,6 +150,7 @@ var WebAudioSoundManager = new Class({
145150
{
146151
document.body.removeEventListener('touchstart', unlock);
147152
document.body.removeEventListener('touchend', unlock);
153+
148154
_this.unlocked = true;
149155
});
150156
};

0 commit comments

Comments
 (0)