From cc06a62b9019953dc1d1fdd3f57012822a18cb92 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Tue, 25 Feb 2014 03:12:12 +0000 Subject: [PATCH] Pausing the game will now mute audio and resuming will un-mute, unless it was muted via the game (fixes #439) --- README.md | 2 + examples/wip/tab swap.js | 15 ++++++- src/core/Game.js | 4 +- src/sound/SoundManager.js | 93 ++++++++++++++++++++++++++------------- 4 files changed, 80 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 5950f3e3..3faeb6b7 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ Updates: * Updated the way the page visibility is checked, should now be more compatible across more browsers. * Phaser.Input.Key.isUp now defaults to 'true', as does GamepadButton.isUp (#474) * Vastly improved visibility API support + pageshow/pagehide + focus/blur. Working across Chrome, IE, Firefox, iOS, Android (also fixes #161) +* Pausing the game will now mute audio and resuming will un-mute, unless it was muted via the game (fixes #439) Bug Fixes: @@ -152,6 +153,7 @@ Bug Fixes: * Text that was fixedToCamera would 'jitter' if the world scrolled. Now works as expected across all fixed objects. * Fixed a bug where Sound.play wouldn't pick-up the local loop setting if not specified in the parameter. * Active animations now monitor if the game pauses, and resume normally when the game un-pauses (fixes #179) +* Swapping between tabs will now pause the game correctly on mobile browsers (iOS7+) TO DO: diff --git a/examples/wip/tab swap.js b/examples/wip/tab swap.js index 78da6891..a8ecb503 100644 --- a/examples/wip/tab swap.js +++ b/examples/wip/tab swap.js @@ -31,12 +31,25 @@ function create() { game.onResume.add(resumed, this); var space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); - space.onDown.add(pauseToggle, this); + space.onDown.add(muteToggle, this); s.push('starting: ' + game.stage._hiddenVar); } +function muteToggle() { + + if (game.sound.mute) + { + game.sound.mute = false; + } + else + { + game.sound.mute = true; + } + +} + function pauseToggle() { if (game.paused) diff --git a/src/core/Game.js b/src/core/Game.js index 83c09b93..f2333726 100644 --- a/src/core/Game.js +++ b/src/core/Game.js @@ -736,7 +736,7 @@ Phaser.Game.prototype = { { this._paused = true; this.time.gamePaused(time); - this.sound.mute = true; + this.sound.setMute(); this.onPause.dispatch(this); } @@ -755,7 +755,7 @@ Phaser.Game.prototype = { this._paused = false; this.time.gameResumed(time); this.input.reset(); - this.sound.mute = false; + this.sound.unsetMute(); this.onResume.dispatch(this); } diff --git a/src/sound/SoundManager.js b/src/sound/SoundManager.js index 2b7cd833..85d506cc 100644 --- a/src/sound/SoundManager.js +++ b/src/sound/SoundManager.js @@ -26,6 +26,13 @@ Phaser.SoundManager = function (game) { */ this.onSoundDecode = new Phaser.Signal(); + /** + * @property {boolean} _codeMuted - Internal mute tracking var. + * @private + * @default + */ + this._codeMuted = false; + /** * @property {boolean} _muted - Internal mute tracking var. * @private @@ -362,6 +369,57 @@ Phaser.SoundManager.prototype = { return sound; + }, + + setMute: function () { + + if (this._muted) + { + return; + } + + this._muted = true; + + if (this.usingWebAudio) + { + this._muteVolume = this.masterGain.gain.value; + this.masterGain.gain.value = 0; + } + + // Loop through sounds + for (var i = 0; i < this._sounds.length; i++) + { + if (this._sounds[i].usingAudioTag) + { + this._sounds[i].mute = true; + } + } + + }, + + unsetMute: function () { + + if (!this._muted || this._codeMuted) + { + return; + } + + this._muted = false; + + if (this.usingWebAudio) + { + this.masterGain.gain.value = this._muteVolume; + } + + // Loop through sounds + for (var i = 0; i < this._sounds.length; i++) + { + if (this._sounds[i].usingAudioTag) + { + this._sounds[i].mute = false; + } + } + } }; @@ -391,22 +449,8 @@ Object.defineProperty(Phaser.SoundManager.prototype, "mute", { return; } - this._muted = true; - - if (this.usingWebAudio) - { - this._muteVolume = this.masterGain.gain.value; - this.masterGain.gain.value = 0; - } - - // Loop through sounds - for (var i = 0; i < this._sounds.length; i++) - { - if (this._sounds[i].usingAudioTag) - { - this._sounds[i].mute = true; - } - } + this._codeMuted = true; + this.setMute(); } else { @@ -415,21 +459,8 @@ Object.defineProperty(Phaser.SoundManager.prototype, "mute", { return; } - this._muted = false; - - if (this.usingWebAudio) - { - this.masterGain.gain.value = this._muteVolume; - } - - // Loop through sounds - for (var i = 0; i < this._sounds.length; i++) - { - if (this._sounds[i].usingAudioTag) - { - this._sounds[i].mute = false; - } - } + this._codeMuted = false; + this.unsetMute(); } }