Skip to content

Commit cc06a62

Browse files
committed
Pausing the game will now mute audio and resuming will un-mute, unless it was muted via the game (fixes phaserjs#439)
1 parent 415342d commit cc06a62

4 files changed

Lines changed: 80 additions & 34 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Updates:
132132
* Updated the way the page visibility is checked, should now be more compatible across more browsers.
133133
* Phaser.Input.Key.isUp now defaults to 'true', as does GamepadButton.isUp (#474)
134134
* Vastly improved visibility API support + pageshow/pagehide + focus/blur. Working across Chrome, IE, Firefox, iOS, Android (also fixes #161)
135+
* Pausing the game will now mute audio and resuming will un-mute, unless it was muted via the game (fixes #439)
135136

136137

137138
Bug Fixes:
@@ -152,6 +153,7 @@ Bug Fixes:
152153
* Text that was fixedToCamera would 'jitter' if the world scrolled. Now works as expected across all fixed objects.
153154
* Fixed a bug where Sound.play wouldn't pick-up the local loop setting if not specified in the parameter.
154155
* Active animations now monitor if the game pauses, and resume normally when the game un-pauses (fixes #179)
156+
* Swapping between tabs will now pause the game correctly on mobile browsers (iOS7+)
155157

156158

157159
TO DO:

examples/wip/tab swap.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,25 @@ function create() {
3131
game.onResume.add(resumed, this);
3232

3333
var space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
34-
space.onDown.add(pauseToggle, this);
34+
space.onDown.add(muteToggle, this);
3535

3636
s.push('starting: ' + game.stage._hiddenVar);
3737

3838
}
3939

40+
function muteToggle() {
41+
42+
if (game.sound.mute)
43+
{
44+
game.sound.mute = false;
45+
}
46+
else
47+
{
48+
game.sound.mute = true;
49+
}
50+
51+
}
52+
4053
function pauseToggle() {
4154

4255
if (game.paused)

src/core/Game.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ Phaser.Game.prototype = {
736736
{
737737
this._paused = true;
738738
this.time.gamePaused(time);
739-
this.sound.mute = true;
739+
this.sound.setMute();
740740
this.onPause.dispatch(this);
741741
}
742742

@@ -755,7 +755,7 @@ Phaser.Game.prototype = {
755755
this._paused = false;
756756
this.time.gameResumed(time);
757757
this.input.reset();
758-
this.sound.mute = false;
758+
this.sound.unsetMute();
759759
this.onResume.dispatch(this);
760760
}
761761

src/sound/SoundManager.js

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Phaser.SoundManager = function (game) {
2626
*/
2727
this.onSoundDecode = new Phaser.Signal();
2828

29+
/**
30+
* @property {boolean} _codeMuted - Internal mute tracking var.
31+
* @private
32+
* @default
33+
*/
34+
this._codeMuted = false;
35+
2936
/**
3037
* @property {boolean} _muted - Internal mute tracking var.
3138
* @private
@@ -362,6 +369,57 @@ Phaser.SoundManager.prototype = {
362369

363370
return sound;
364371

372+
},
373+
374+
setMute: function () {
375+
376+
if (this._muted)
377+
{
378+
return;
379+
}
380+
381+
this._muted = true;
382+
383+
if (this.usingWebAudio)
384+
{
385+
this._muteVolume = this.masterGain.gain.value;
386+
this.masterGain.gain.value = 0;
387+
}
388+
389+
// Loop through sounds
390+
for (var i = 0; i < this._sounds.length; i++)
391+
{
392+
if (this._sounds[i].usingAudioTag)
393+
{
394+
this._sounds[i].mute = true;
395+
}
396+
}
397+
398+
},
399+
400+
unsetMute: function () {
401+
402+
if (!this._muted || this._codeMuted)
403+
{
404+
return;
405+
}
406+
407+
this._muted = false;
408+
409+
if (this.usingWebAudio)
410+
{
411+
this.masterGain.gain.value = this._muteVolume;
412+
}
413+
414+
// Loop through sounds
415+
for (var i = 0; i < this._sounds.length; i++)
416+
{
417+
if (this._sounds[i].usingAudioTag)
418+
{
419+
this._sounds[i].mute = false;
420+
}
421+
}
422+
365423
}
366424

367425
};
@@ -391,22 +449,8 @@ Object.defineProperty(Phaser.SoundManager.prototype, "mute", {
391449
return;
392450
}
393451

394-
this._muted = true;
395-
396-
if (this.usingWebAudio)
397-
{
398-
this._muteVolume = this.masterGain.gain.value;
399-
this.masterGain.gain.value = 0;
400-
}
401-
402-
// Loop through sounds
403-
for (var i = 0; i < this._sounds.length; i++)
404-
{
405-
if (this._sounds[i].usingAudioTag)
406-
{
407-
this._sounds[i].mute = true;
408-
}
409-
}
452+
this._codeMuted = true;
453+
this.setMute();
410454
}
411455
else
412456
{
@@ -415,21 +459,8 @@ Object.defineProperty(Phaser.SoundManager.prototype, "mute", {
415459
return;
416460
}
417461

418-
this._muted = false;
419-
420-
if (this.usingWebAudio)
421-
{
422-
this.masterGain.gain.value = this._muteVolume;
423-
}
424-
425-
// Loop through sounds
426-
for (var i = 0; i < this._sounds.length; i++)
427-
{
428-
if (this._sounds[i].usingAudioTag)
429-
{
430-
this._sounds[i].mute = false;
431-
}
432-
}
462+
this._codeMuted = false;
463+
this.unsetMute();
433464
}
434465
}
435466

0 commit comments

Comments
 (0)