Navigation
These archived docs are for Phaser 2.4.2 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser. SoundManager

new SoundManager(game)

The Sound Manager is responsible for playing back audio via either the Legacy HTML Audio tag or via Web Audio if the browser supports it. Note: On Firefox 25+ on Linux if you have media.gstreamer disabled in about:config then it cannot play back mp3 or m4a files. The audio file type and the encoding of those files are extremely important. Not all browsers can play all audio formats. There is a good guide to what's supported here: http://hpr.dogphilosophy.net/test/

If you are reloading a Phaser Game on a page that never properly refreshes (such as in an AngularJS project) then you will quickly run out of AudioContext nodes. If this is the case create a global var called PhaserGlobal on the window object before creating the game. The active AudioContext will then be saved to window.PhaserGlobal.audioContext when the Phaser game is destroyed, and re-used when it starts again.

Mobile warning: There are some mobile devices (certain iPad 2 and iPad Mini revisions) that cannot play 48000 Hz audio. When they try to play the audio becomes extremely distorted and buzzes, eventually crashing the sound system. The solution is to use a lower encoding rate such as 44100 Hz.

Parameters
Name Type Description
game Phaser.Game

Reference to the current game instance.

Source code: sound/SoundManager.js (Line 25)

Public Properties

channels : number

The number of audio channels to use in playback.

Default Value
  • 32
Source code: sound/SoundManager.js (Line 95)

connectToMaster : boolean

Used in conjunction with Sound.externalNode this allows you to stop a Sound node being connected to the SoundManager master gain node.

Default Value
  • true
Source code: sound/SoundManager.js (Line 83)

context :AudioContext

The AudioContext being used for playback.

Default Value
  • null
Source code: sound/SoundManager.js (Line 59)

game : Phaser.Game

Local reference to game.

Source code: sound/SoundManager.js (Line 30)

mute : boolean

Gets or sets the muted state of the SoundManager. This effects all sounds in the game.

Source code: sound/SoundManager.js (Line 732)

noAudio : boolean

True if audio been disabled via the PhaserGlobal (useful if you need to use a 3rd party audio library) or the device doesn't support any audio.

Default Value
  • false
Source code: sound/SoundManager.js (Line 77)

onMute : Phaser.Signal

This signal is dispatched when the SoundManager is globally muted, either directly via game code or as a result of the game pausing.

Source code: sound/SoundManager.js (Line 47)

onSoundDecode : Phaser.Signal

The event dispatched when a sound decodes (typically only for mp3 files)

Source code: sound/SoundManager.js (Line 35)

onUnMute : Phaser.Signal

This signal is dispatched when the SoundManager is globally un-muted, either directly via game code or as a result of the game resuming from a pause.

Source code: sound/SoundManager.js (Line 53)

onVolumeChange : Phaser.Signal

This signal is dispatched whenever the global volume changes. The new volume is passed as the only parameter to your callback.

Source code: sound/SoundManager.js (Line 41)

touchLocked : boolean

true if the audio system is currently locked awaiting a touch event.

Default Value
  • false
Source code: sound/SoundManager.js (Line 89)

[readonly] usingAudioTag : boolean

True the SoundManager and device are both using the Audio tag instead of Web Audio.

Source code: sound/SoundManager.js (Line 71)

[readonly] usingWebAudio : boolean

True the SoundManager and device are both using Web Audio.

Source code: sound/SoundManager.js (Line 65)

volume : number

Gets or sets the global volume of the SoundManager, a value between 0 and 1. The value given is clamped to the range 0 to 1.

Source code: sound/SoundManager.js (Line 772)

Public Methods

add(key, volume, loop, connect) → {Phaser.Sound}

Adds a new Sound into the SoundManager.

Parameters
Name Type Argument Default Description
key string

Asset key for the sound.

volume number <optional>
1

Default value for the volume.

loop boolean <optional>
false

Whether or not the sound will loop.

connect boolean <optional>
true

Controls if the created Sound object will connect to the master gainNode of the SoundManager when running under WebAudio.

Returns

The new sound instance.

Source code: sound/SoundManager.js (Line 517)

addSprite(key) → {Phaser.AudioSprite}

Adds a new AudioSprite into the SoundManager.

Parameters
Name Type Description
key string

Asset key for the sound.

Returns

The new AudioSprite instance.

Source code: sound/SoundManager.js (Line 541)

<internal> boot()

Initialises the sound manager.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: sound/SoundManager.js (Line 159)

decode(key, sound)

Decode a sound by its asset key.

Parameters
Name Type Argument Description
key string

Assets key of the sound to be decoded.

sound Phaser.Sound <optional>

Its buffer will be set to decoded data.

Source code: sound/SoundManager.js (Line 383)

destroy()

Stops all the sounds in the game, then destroys them and finally clears up any callbacks.

Source code: sound/SoundManager.js (Line 699)

pauseAll()

Pauses all the sounds in the game.

Source code: sound/SoundManager.js (Line 339)

play(key, volume, loop) → {Phaser.Sound}

Adds a new Sound into the SoundManager and starts it playing.

Parameters
Name Type Argument Default Description
key string

Asset key for the sound.

volume number <optional>
1

Default value for the volume.

loop boolean <optional>
false

Whether or not the sound will loop.

Returns

The new sound instance.

Source code: sound/SoundManager.js (Line 608)

remove(sound) → {boolean}

Removes a Sound from the SoundManager. The removed Sound is destroyed before removal.

Parameters
Name Type Description
sound Phaser.Sound

The sound object to remove.

Returns
boolean -

True if the sound was removed successfully, otherwise false.

Source code: sound/SoundManager.js (Line 556)

removeByKey(key) → {number}

Removes all Sounds from the SoundManager that have an asset key matching the given value. The removed Sounds are destroyed before removal.

Parameters
Name Type Description
key string

The key to match when removing sound objects.

Returns
number -

The number of matching sound objects that were removed.

Source code: sound/SoundManager.js (Line 581)

resumeAll()

Resumes every sound in the game.

Source code: sound/SoundManager.js (Line 361)

setDecodedCallback(files, callback, callbackContext)

This method allows you to give the SoundManager a list of Sound files, or keys, and a callback. Once all of the Sound files have finished decoding the callback will be invoked. The amount of time spent decoding depends on the codec used and file size. If all of the files given have already decoded the callback is triggered immediately.

Parameters
Name Type Description
files string | array

An array containing either Phaser.Sound objects or their key strings as found in the Phaser.Cache.

callback function

The callback which will be invoked once all files have finished decoding.

callbackContext Object

The context in which the callback will run.

Source code: sound/SoundManager.js (Line 420)

setTouchLock()

Sets the Input Manager touch callback to be SoundManager.unlock. Required for iOS audio device unlocking. Mostly just used internally.

Source code: sound/SoundManager.js (Line 260)

stopAll()

Stops all the sounds in the game.

Source code: sound/SoundManager.js (Line 317)

unlock() → {boolean}

Enables the audio, usually after the first touch.

Returns
boolean -

True if the callback should be removed, otherwise false.

Source code: sound/SoundManager.js (Line 273)

<internal> update()

Updates every sound in the game, checks for audio unlock on mobile and monitors the decoding watch list.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: sound/SoundManager.js (Line 470)