Class: SoundManager

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. Sometimes the audio context will be created with a sampleRate of 48000. If this happens and audio distorts you should re-create the context.

Parameters:
Name Type Description
game Phaser.Game

Reference to the current game instance.

Source:
src/sound/SoundManager.js line 26

Members

baseLatency : number

The AudioContext's processing latency (or an estimate thereof), in seconds. This could be useful for scheduling playback very precisely. If not using Web Audio, this will be null.

Type:
  • number
Source:
src/sound/SoundManager.js line 83
See:

channels : number

The number of audio channels to use in playback.

Type:
  • number
Default Value:
  • 32
Source:
src/sound/SoundManager.js line 119

connectToMaster : boolean

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

Type:
  • boolean
Default Value:
  • true
Source:
src/sound/SoundManager.js line 107

context : AudioContext

The AudioContext being used for playback.

Type:
  • AudioContext
Source:
src/sound/SoundManager.js line 73

game : Phaser.Game

Local reference to game.

Type:
Source:
src/sound/SoundManager.js line 31

mute : boolean

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

Type:
  • boolean
Source:
src/sound/SoundManager.js line 925

muteOnPause : boolean

Set to true to have all sound muted when the Phaser game pauses (such as on loss of focus), or set to false to keep audio playing, regardless of the game pause state. You may need to do this should you wish to control audio muting via external DOM buttons or similar.

Type:
  • boolean
Default Value:
  • true
Source:
src/sound/SoundManager.js line 128

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.

Type:
  • boolean
Source:
src/sound/SoundManager.js line 101

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.

Type:
Source:
src/sound/SoundManager.js line 48

onSoundDecode : Phaser.Signal

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

Type:
Source:
src/sound/SoundManager.js line 36

onStateChange : Phaser.Signal

This signal is dispatched when the AudioContext state changes, only if using Web Audio.

Type:
Source:
src/sound/SoundManager.js line 67

onTouchUnlock : Phaser.Signal

This signal is dispatched when the SoundManager is touch-unlocked. If the device doesn't need touch-unlocking, the signal won't be dispatched.

Type:
Source:
src/sound/SoundManager.js line 61

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.

Type:
Source:
src/sound/SoundManager.js line 54

onVolumeChange : Phaser.Signal

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

Type:
Source:
src/sound/SoundManager.js line 42

touchLocked : boolean

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

Type:
  • boolean
Source:
src/sound/SoundManager.js line 113

<readonly> usingAudioTag : boolean

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

Type:
  • boolean
Source:
src/sound/SoundManager.js line 95

<readonly> usingWebAudio : boolean

True the SoundManager and device are both using Web Audio.

Type:
  • boolean
Source:
src/sound/SoundManager.js line 89

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.

Type:
  • number
Source:
src/sound/SoundManager.js line 964

Methods

add(key [, volume] [, loop] [, connect])

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.

Source:
src/sound/SoundManager.js line 642
Returns:

The new sound instance.

Type
Phaser.Sound

addSprite(key)

Adds a new AudioSprite into the SoundManager.

Parameters:
Name Type Description
key string

Asset key for the sound.

Source:
src/sound/SoundManager.js line 665
Returns:

The new AudioSprite instance.

Type
Phaser.AudioSprite

<protected> boot()

Initialises the sound manager.

Source:
src/sound/SoundManager.js line 203

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:
src/sound/SoundManager.js line 504

destroy()

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

Source:
src/sound/SoundManager.js line 864

pauseAll()

Pauses all the sounds in the game.

Source:
src/sound/SoundManager.js line 462

play(key [, volume] [, loop])

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.

Source:
src/sound/SoundManager.js line 750
Returns:

The new sound instance.

Type
Phaser.Sound

remove(sound)

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.

Source:
src/sound/SoundManager.js line 679
Returns:

True if the sound was removed successfully, otherwise false.

Type
boolean

removeAll()

Removes all Sounds from the SoundManager. The removed Sounds are destroyed before removal.

Source:
src/sound/SoundManager.js line 703

removeByKey(key)

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.

Source:
src/sound/SoundManager.js line 724
Returns:

The number of matching sound objects that were removed.

Type
number

resumeAll()

Resumes every sound in the game.

Source:
src/sound/SoundManager.js line 483

resumeWebAudio()

Try to resume the Web Audio context, if using Web Audio.

Source:
src/sound/SoundManager.js line 365
Returns:
Type
Promise

resumeWebAudioIfSuspended()

Try to resume a suspended Web Audio context, if using Web Audio and the context is suspended.

Source:
src/sound/SoundManager.js line 378
Returns:
Type
Promise

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:
src/sound/SoundManager.js line 548

setTouchLock()

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

Source:
src/sound/SoundManager.js line 319

stopAll()

Stops all the sounds in the game.

Source:
src/sound/SoundManager.js line 441

unlock()

Enables the audio, usually after the first touch.

Source:
src/sound/SoundManager.js line 386
Returns:

True if the callback should be removed, otherwise false.

Type
boolean

<protected> update()

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

Source:
src/sound/SoundManager.js line 597

webAudioIsSuspended()

Is the Web Audio context suspended?

Source:
src/sound/SoundManager.js line 355
Returns:
Type
boolean

phaser-ce@2.20.0 is on GitHub and NPM

Documentation generated by JSDoc 3.6.7 on 2022-12-10 using Tomorrow.