Skip to content

Commit 9882906

Browse files
committed
When setting a global volume for the SoundManager it would previously incorrectly calculate the volumes of AudioTag based Sound objects that were not played at volume 1. The new approach uses Sound.updateGlobalVolume which adjusts the Sound volume to be a percentage of the global volume. So if the global volume is 0.5 and the Sound volume is 0.5, the Sound will play with an actual volume of 0.25 (thanks @VitaZheltyakov phaserjs#2325)
1 parent 71056cc commit 9882906

3 files changed

Lines changed: 56 additions & 32 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
347347
* Camera.follow now uses the Targets `world` property to seed the camera coordinates from, rather than its local position. This means Sprites that are members of offset Groups, or transformed display lists, should now be followed more accurately (thanks @rbozan #2106)
348348
* PluginManager.destroy is now called by Game.destroy.
349349
* Game.forceSingleUpdate is now `true` by default.
350+
* Video now uses MediaStreamTrack.stop() instead of MediaStream.stop() where possible, as the later is now deprecated in some browsers (thanks @stoneman1 #2371)
350351

351352
### Bug Fixes
352353

@@ -364,6 +365,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
364365
* Arcade.Body's speed property was only set when the body moved, it now updates regardless (thanks @mark-henry #2417)
365366
* Camera.position would return the view rectangles centerX/Y coordinates, instead of view.x/y (which is what Camera.x/y returns), so it has been updated to return view.x/y instead (thanks @kamparR #2120)
366367
* Passing a BitmapData to a TileSprite as a texture would fail if the BitmapData had not been previously added to the cache. It now uses the new frameData property (thanks @mzamateo @lucap86 #2380)
368+
* When setting a global volume for the SoundManager it would previously incorrectly calculate the volumes of AudioTag based Sound objects that were not played at volume 1. The new approach uses Sound.updateGlobalVolume which adjusts the Sound volume to be a percentage of the global volume. So if the global volume is 0.5 and the Sound volume is 0.5, the Sound will play with an actual volume of 0.25 (thanks @VitaZheltyakov #2325)
367369

368370
### Pixi Updates
369371

src/sound/Sound.js

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -901,17 +901,17 @@ Phaser.Sound.prototype = {
901901
},
902902

903903
/**
904-
* Starts this sound playing (or restarts it if already doing so) and sets the volume to zero.
905-
* Then increases the volume from 0 to 1 over the duration specified.
906-
*
907-
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
908-
* and the final volume (1) as the second parameter.
909-
*
910-
* @method Phaser.Sound#fadeIn
911-
* @param {number} [duration=1000] - The time in milliseconds over which the Sound should fade in.
912-
* @param {boolean} [loop=false] - Should the Sound be set to loop? Note that this doesn't cause the fade to repeat.
913-
* @param {string} [marker=(current marker)] - The marker to start at; defaults to the current (last played) marker. To start playing from the beginning specify specify a marker of `''`.
914-
*/
904+
* Starts this sound playing (or restarts it if already doing so) and sets the volume to zero.
905+
* Then increases the volume from 0 to 1 over the duration specified.
906+
*
907+
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
908+
* and the final volume (1) as the second parameter.
909+
*
910+
* @method Phaser.Sound#fadeIn
911+
* @param {number} [duration=1000] - The time in milliseconds over which the Sound should fade in.
912+
* @param {boolean} [loop=false] - Should the Sound be set to loop? Note that this doesn't cause the fade to repeat.
913+
* @param {string} [marker=(current marker)] - The marker to start at; defaults to the current (last played) marker. To start playing from the beginning specify specify a marker of `''`.
914+
*/
915915
fadeIn: function (duration, loop, marker) {
916916

917917
if (loop === undefined) { loop = false; }
@@ -929,28 +929,28 @@ Phaser.Sound.prototype = {
929929
},
930930

931931
/**
932-
* Decreases the volume of this Sound from its current value to 0 over the duration specified.
933-
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
934-
* and the final volume (0) as the second parameter.
935-
*
936-
* @method Phaser.Sound#fadeOut
937-
* @param {number} [duration=1000] - The time in milliseconds over which the Sound should fade out.
938-
*/
932+
* Decreases the volume of this Sound from its current value to 0 over the duration specified.
933+
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
934+
* and the final volume (0) as the second parameter.
935+
*
936+
* @method Phaser.Sound#fadeOut
937+
* @param {number} [duration=1000] - The time in milliseconds over which the Sound should fade out.
938+
*/
939939
fadeOut: function (duration) {
940940

941941
this.fadeTo(duration, 0);
942942

943943
},
944944

945945
/**
946-
* Fades the volume of this Sound from its current value to the given volume over the duration specified.
947-
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
948-
* and the final volume (volume) as the second parameter.
949-
*
950-
* @method Phaser.Sound#fadeTo
951-
* @param {number} [duration=1000] - The time in milliseconds during which the Sound should fade out.
952-
* @param {number} [volume] - The volume which the Sound should fade to. This is a value between 0 and 1.
953-
*/
946+
* Fades the volume of this Sound from its current value to the given volume over the duration specified.
947+
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
948+
* and the final volume (volume) as the second parameter.
949+
*
950+
* @method Phaser.Sound#fadeTo
951+
* @param {number} [duration=1000] - The time in milliseconds during which the Sound should fade out.
952+
* @param {number} [volume] - The volume which the Sound should fade to. This is a value between 0 and 1.
953+
*/
954954
fadeTo: function (duration, volume) {
955955

956956
if (!this.isPlaying || this.paused || volume === this.volume)
@@ -973,11 +973,11 @@ Phaser.Sound.prototype = {
973973
},
974974

975975
/**
976-
* Internal handler for Sound.fadeIn, Sound.fadeOut and Sound.fadeTo.
977-
*
978-
* @method Phaser.Sound#fadeComplete
979-
* @private
980-
*/
976+
* Internal handler for Sound.fadeIn, Sound.fadeOut and Sound.fadeTo.
977+
*
978+
* @method Phaser.Sound#fadeComplete
979+
* @private
980+
*/
981981
fadeComplete: function () {
982982

983983
this.onFadeComplete.dispatch(this, this.volume);
@@ -989,6 +989,28 @@ Phaser.Sound.prototype = {
989989

990990
},
991991

992+
/**
993+
* Called automatically by SoundManager.volume.
994+
*
995+
* Sets the volume of AudioTag Sounds as a percentage of the Global Volume.
996+
*
997+
* You should not normally call this directly.
998+
*
999+
* @method Phaser.Sound#updateGlobalVolume
1000+
* @protected
1001+
* @param {float} globalVolume - The global SoundManager volume.
1002+
*/
1003+
updateGlobalVolume: function (globalVolume) {
1004+
1005+
// this._volume is the % of the global volume this sound should be played at
1006+
1007+
if (this.usingAudioTag && this._sound)
1008+
{
1009+
this._sound.volume = globalVolume * this._volume;
1010+
}
1011+
1012+
},
1013+
9921014
/**
9931015
* Destroys this sound and all associated events and removes it from the SoundManager.
9941016
*

src/sound/SoundManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ Object.defineProperty(Phaser.SoundManager.prototype, "volume", {
830830
{
831831
if (this._sounds[i].usingAudioTag)
832832
{
833-
this._sounds[i].volume = value;
833+
this._sounds[i].updateGlobalVolume(value);
834834
}
835835
}
836836
}

0 commit comments

Comments
 (0)