Pausing the game will now mute audio and resuming will un-mute, unless it was muted via the game (fixes #439)

This commit is contained in:
photonstorm
2014-02-25 03:12:12 +00:00
parent 415342d986
commit cc06a62b90
4 changed files with 80 additions and 34 deletions
+2 -2
View File
@@ -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);
}
+62 -31
View File
@@ -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();
}
}