Phaser API Documentation

  Version: 
Filter
This is a beta release of our new docs system. Found an issue?
Please tell us about it in the #📖-newdocs-feedback channel on the Phaser Discord

Phaser . Sound . BaseSoundManager

This is a beta release of our new docs system. Found an issue?
Please tell us about it in the #📖-newdocs-feedback channel on the Phaser Discord

Phaser . Sound . BaseSoundManager

Base class for other Sound Manager classes.

Constructor:

new BaseSoundManager(game)

Parameters:

name type description
game Phaser.Game

Reference to the current game instance.

Since: 3.0.0

Extends


Members

<private> _detune: number
Focus
Focus

Description:

Property that actually holds the value of global detune.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
<private> _rate: number
Focus
Focus

Description:

Property that actually holds the value of global playback rate.

Type:
number
Default: 1
Since: 3.0.0
Focus
Focus
detune: number
Focus
Focus

Description:

Global detuning of all sounds in cents. The range of the value is -1200 to 1200, but we recommend setting it to 50.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
<readonly> game: Phaser.Game
Focus
Focus

Description:

Local reference to game.

Type:
Since: 3.0.0
Focus
Focus
<readonly> jsonCache: Phaser.Cache.BaseCache
Focus
Focus

Description:

Local reference to the JSON Cache, as used by Audio Sprites.

Type:
Since: 3.7.0
Focus
Focus
<readonly> locked: boolean
Focus
Focus

Description:

Mobile devices require sounds to be triggered from an explicit user action, such as a tap, before any sound can be loaded/played on a web page. Set to true if the audio system is currently locked awaiting user interaction.

Type:
boolean
Since: 3.0.0
Focus
Focus
mute: boolean
Focus
Focus

Description:

Global mute setting.

Type:
boolean
Default: false
Since: 3.0.0
Focus
Focus
pauseOnBlur: boolean
Focus
Focus

Description:

Flag indicating if sounds should be paused when game looses focus, for instance when user switches to another tab/program/app.

Type:
boolean
Default: true
Since: 3.0.0
Focus
Focus
rate: number
Focus
Focus

Description:

Global playback rate at which all the sounds will be played. Value of 1.0 plays the audio at full speed, 0.5 plays the audio at half speed and 2.0 doubles the audio's playback speed.

Type:
number
Default: 1
Since: 3.0.0
Focus
Focus
<private> sounds: Array.<Phaser.Sound.BaseSound>
Focus
Focus

Description:

An array containing all added sounds.

Type:
Default: []
Since: 3.0.0
Focus
Focus
<private> unlocked: boolean
Focus
Focus

Description:

Flag used internally for handling when the audio system has been unlocked, if there ever was a need for it.

Type:
boolean
Default: false
Since: 3.0.0
Focus
Focus
volume: number
Focus
Focus

Description:

Global volume setting.

Type:
number
Default: 1
Since: 3.0.0
Focus
Focus

Methods

add(key, [config])
Focus
Focus

Description:

Adds a new sound into the sound manager.

Parameters:

name type arguments description
key string

Asset key for the sound.

config Phaser.Types.Sound.SoundConfig <optional>

An optional config object containing default sound settings.

Returns:
Description:

The new sound instance.

Examples:
Since: 3.0.0
Focus
Focus
addAudioSprite(key, [config])
Focus
Focus

Description:

Adds a new audio sprite sound into the sound manager. Audio Sprites are a combination of audio files and a JSON configuration. The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite

Parameters:

name type arguments description
key string

Asset key for the sound.

config Phaser.Types.Sound.SoundConfig <optional>

An optional config object containing default sound settings.

Returns:
Description:

The new audio sprite sound instance.

Examples:
Since: 3.0.0
Focus
Focus
addListener(event, fn, [context])
Focus
Focus

Description:

Add a listener for a given event.

Parameters:

name type arguments Default description
event string | symbol

The event name.

fn function

The listener function.

context * <optional> this

The context to invoke the listener with.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#addListener
Examples:
Since: 3.0.0
Focus
Focus
destroy()
Focus
Focus

Description:

Destroys all the sounds in the game and all associated events.

Overrides: Phaser.Events.EventEmitter#destroy
Examples:
Since: 3.0.0
Focus
Focus
emit(event, [args])
Focus
Focus

Description:

Calls each of the listeners registered for a given event.

Parameters:

name type arguments description
event string | symbol

The event name.

args * <optional>

Additional arguments that will be passed to the event handler.

Returns:
Description:

true if the event had listeners, else false.

Type:
  • boolean
Inherited from: Phaser.Events.EventEmitter#emit
Examples:
Since: 3.0.0
Focus
Focus
eventNames()
Focus
Focus

Description:

Return an array listing the events for which the emitter has registered listeners.

Type:
  • Array.<(string
  • symbol)>
Inherited from: Phaser.Events.EventEmitter#eventNames
Examples:
Since: 3.0.0
Focus
Focus
<private> forEachActiveSound(callback, [scope])
Focus
Focus

Description:

Method used internally for iterating only over active sounds and skipping sounds that are marked for removal.

Parameters:

name type arguments description
callback Phaser.Types.Sound.EachActiveSoundCallback

Callback function. (manager: Phaser.Sound.BaseSoundManager, sound: Phaser.Sound.BaseSound, index: number, sounds: Phaser.Manager.BaseSound[]) => void

scope * <optional>

Callback context.

Examples:
Since: 3.0.0
Focus
Focus
get(key)
Focus
Focus

Description:

Gets the first sound in the manager matching the given key, if any.

Parameters:

name type description
key string

Sound asset key.

Returns:
Description:
  • The sound, or null.
Examples:
Since: 3.23.0
Focus
Focus
getAll(key)
Focus
Focus

Description:

Gets any sounds in the manager matching the given key.

Parameters:

name type description
key string

Sound asset key.

Returns:
Description:
  • The sounds, or an empty array.
Type:
Examples:
Since: 3.23.0
Focus
Focus
listenerCount(event)
Focus
Focus

Description:

Return the number of listeners listening to a given event.

Parameters:

name type description
event string | symbol

The event name.

Returns:
Description:

The number of listeners.

Type:
  • number
Inherited from: Phaser.Events.EventEmitter#listenerCount
Examples:
Since: 3.0.0
Focus
Focus
listeners(event)
Focus
Focus

Description:

Return the listeners registered for a given event.

Parameters:

name type description
event string | symbol

The event name.

Returns:
Description:

The registered listeners.

Type:
  • Array.
Inherited from: Phaser.Events.EventEmitter#listeners
Examples:
Since: 3.0.0
Focus
Focus
off(event, [fn], [context], [once])
Focus
Focus

Description:

Remove the listeners of a given event.

Parameters:

name type arguments description
event string | symbol

The event name.

fn function <optional>

Only remove the listeners that match this function.

context * <optional>

Only remove the listeners that have this context.

once boolean <optional>

Only remove one-time listeners.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#off
Examples:
Since: 3.0.0
Focus
Focus
on(event, fn, [context])
Focus
Focus

Description:

Add a listener for a given event.

Parameters:

name type arguments Default description
event string | symbol

The event name.

fn function

The listener function.

context * <optional> this

The context to invoke the listener with.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#on
Examples:
Since: 3.0.0
Focus
Focus
<protected> onBlur()
Focus
Focus

Description:

Method used internally for pausing sound manager if Phaser.Sound.BaseSoundManager#pauseOnBlur is set to true.

Examples:
Since: 3.0.0
Focus
Focus
<protected> onFocus()
Focus
Focus

Description:

Method used internally for resuming sound manager if Phaser.Sound.BaseSoundManager#pauseOnBlur is set to true.

Examples:
Since: 3.0.0
Focus
Focus
<private> onGameBlur()
Focus
Focus

Description:

Internal handler for Phaser.Core.Events#BLUR.

Examples:
Since: 3.23.0
Focus
Focus
<private> onGameFocus()
Focus
Focus

Description:

Internal handler for Phaser.Core.Events#FOCUS.

Examples:
Since: 3.23.0
Focus
Focus
once(event, fn, [context])
Focus
Focus

Description:

Add a one-time listener for a given event.

Parameters:

name type arguments Default description
event string | symbol

The event name.

fn function

The listener function.

context * <optional> this

The context to invoke the listener with.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#once
Examples:
Since: 3.0.0
Focus
Focus
pauseAll()
Focus
Focus

Description:

Pauses all the sounds in the game.

Examples:
Since: 3.0.0
Focus
Focus
play(key, [extra])
Focus
Focus

Description:

Adds a new sound to the sound manager and plays it. The sound will be automatically removed (destroyed) once playback ends. This lets you play a new sound on the fly without the need to keep a reference to it.

Parameters:

name type arguments description
key string

Asset key for the sound.

extra Phaser.Types.Sound.SoundConfig | Phaser.Types.Sound.SoundMarker <optional>

An optional additional object containing settings to be applied to the sound. It could be either config or marker object.

Returns:
Description:

Whether the sound started playing successfully.

Type:
  • boolean
Examples:
Since: 3.0.0
Focus
Focus
playAudioSprite(key, spriteName, [config])
Focus
Focus

Description:

Adds a new audio sprite sound to the sound manager and plays it. The sprite will be automatically removed (destroyed) once playback ends. This lets you play a new sound on the fly without the need to keep a reference to it.

Parameters:

name type arguments description
key string

Asset key for the sound.

spriteName string

The name of the sound sprite to play.

config Phaser.Types.Sound.SoundConfig <optional>

An optional config object containing default sound settings.

Returns:
Description:

Whether the audio sprite sound started playing successfully.

Type:
  • boolean
Examples:
Since: 3.0.0
Focus
Focus
remove(sound)
Focus
Focus

Description:

Removes a sound from the sound manager. The removed sound is destroyed before removal.

Parameters:

name type description
sound Phaser.Sound.BaseSound

The sound object to remove.

Returns:
Description:

True if the sound was removed successfully, otherwise false.

Type:
  • boolean
Examples:
Since: 3.0.0
Focus
Focus
removeAll()
Focus
Focus

Description:

Removes all sounds from the manager, destroying the sounds.

Examples:
Since: 3.23.0
Focus
Focus
removeAllListeners([event])
Focus
Focus

Description:

Remove all listeners, or those of the specified event.

Parameters:

name type arguments description
event string | symbol <optional>

The event name.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#removeAllListeners
Examples:
Since: 3.0.0
Focus
Focus
removeByKey(key)
Focus
Focus

Description:

Removes all sounds from the sound manager 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:
Description:

The number of matching sound objects that were removed.

Type:
  • number
Examples:
Since: 3.0.0
Focus
Focus
removeListener(event, [fn], [context], [once])
Focus
Focus

Description:

Remove the listeners of a given event.

Parameters:

name type arguments description
event string | symbol

The event name.

fn function <optional>

Only remove the listeners that match this function.

context * <optional>

Only remove the listeners that have this context.

once boolean <optional>

Only remove one-time listeners.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#removeListener
Examples:
Since: 3.0.0
Focus
Focus
resumeAll()
Focus
Focus

Description:

Resumes all the sounds in the game.

Examples:
Since: 3.0.0
Focus
Focus
setDetune(value)
Focus
Focus

Description:

Sets the global detuning of all sounds in cents. The range of the value is -1200 to 1200, but we recommend setting it to 50.

Parameters:

name type description
value number

The range of the value is -1200 to 1200, but we recommend setting it to 50.

Returns:
Description:

This Sound Manager.

Examples:
Since: 3.3.0
Focus
Focus
setRate(value)
Focus
Focus

Description:

Sets the global playback rate at which all the sounds will be played.

For example, a value of 1.0 plays the audio at full speed, 0.5 plays the audio at half speed and 2.0 doubles the audios playback speed.

Parameters:

name type description
value number

Global playback rate at which all the sounds will be played.

Returns:
Description:

This Sound Manager.

Examples:
Since: 3.3.0
Focus
Focus
shutdown()
Focus
Focus

Description:

Removes all listeners.

Inherited from: Phaser.Events.EventEmitter#shutdown
Examples:
Since: 3.0.0
Focus
Focus
stopAll()
Focus
Focus

Description:

Stops all the sounds in the game.

Examples:
Since: 3.0.0
Focus
Focus
stopByKey(key)
Focus
Focus

Description:

Stops any sounds matching the given key.

Parameters:

name type description
key string

Sound asset key.

Returns:
Description:
  • How many sounds were stopped.
Type:
  • number
Examples:
Since: 3.23.0
Focus
Focus
<protected> unlock()
Focus
Focus

Description:

Method used internally for unlocking audio playback on devices that require user interaction before any sound can be played on a web page.

Read more about how this issue is handled here in this article.

Examples:
Since: 3.0.0
Focus
Focus
<protected> update(time, delta)
Focus
Focus

Description:

Update method called on every game step. Removes destroyed sounds and updates every active sound in the game.

Parameters:

name type description
time number

The current timestamp as generated by the Request Animation Frame or SetTimeout.

delta number

The delta time elapsed since the last frame.

Examples:
Since: 3.0.0
Focus
Focus