Class: HTML5AudioSoundManager

Phaser.Sound. HTML5AudioSoundManager

HTML5AudioSoundManager


new HTML5AudioSoundManager(game)

HTML5 Audio implementation of the Sound Manager.

Parameters:
Name Type Description
game Phaser.Game

Reference to the current game instance.

Since: 3.0.0
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 23)

Extends

Members


audioPlayDelay :number

Value representing time difference, in seconds, between calling
play method on an audio tag and when it actually starts playing.
It is used to achieve more accurate delayed sound playback.

You might need to tweak this value to get the desired results
since audio play delay varies depending on the browser/platform.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0.1
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 44)

detune :number

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
Since: 3.0.0
Inherited From:
Default Value:
  • 0
Source: src/sound/BaseSoundManager.js (Line 629)

<readonly> game :Phaser.Game

Local reference to game.

Type:
Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 57)

<readonly> jsonCache :Phaser.Cache.BaseCache

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

Type:
Since: 3.7.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 67)

<readonly> locked :boolean

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
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 141)

loopEndOffset :number

A value by which we should offset the loop end marker of the
looping sound to compensate for lag, caused by changing audio
tag playback position, in order to achieve gapless looping.

You might need to tweak this value to get the desired results
since loop lag varies depending on the browser/platform.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0.05
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 59)

mute :boolean

Type:
  • boolean
Since: 3.0.0
Overrides:
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 381)

override :boolean

Flag indicating whether if there are no idle instances of HTML5 Audio tag,
for any particular sound, if one of the used tags should be hijacked and used
for succeeding playback or if succeeding Phaser.Sound.HTML5AudioSound#play
call should be ignored.

Type:
  • boolean
Since: 3.0.0
Default Value:
  • true
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 31)

pauseOnBlur :boolean

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

Type:
  • boolean
Since: 3.0.0
Inherited From:
Default Value:
  • true
Source: src/sound/BaseSoundManager.js (Line 108)

rate :number

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
Since: 3.0.0
Inherited From:
Default Value:
  • 1
Source: src/sound/BaseSoundManager.js (Line 573)

volume :number

Type:
  • number
Since: 3.0.0
Overrides:
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 432)

Methods


add(key [, config])

Adds a new sound into the sound manager.

Parameters:
Name Type Argument Description
key string

Asset key for the sound.

config SoundConfig <optional>

An optional config object containing default sound settings.

Since: 3.0.0
Overrides:
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 128)
Returns:

The new sound instance.

Type
Phaser.Sound.HTML5AudioSound

addAudioSprite(key [, config])

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 Argument Description
key string

Asset key for the sound.

config SoundConfig <optional>

An optional config object containing default sound settings.

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 199)
Returns:

The new audio sprite sound instance.

Type
Phaser.Sound.BaseSound.AudioSpriteSound

addListener(event, fn [, context])

Add a listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 111)
Returns:

this.

Type
Phaser.Events.EventEmitter

destroy()

Calls Phaser.Sound.BaseSoundManager#destroy method
and cleans up all HTML5 Audio related stuff.

Since: 3.0.0
Overrides:
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 311)

emit(event [, args])

Calls each of the listeners registered for a given event.

Parameters:
Name Type Argument Description
event string | symbol

The event name.

args * <optional>
<repeatable>

Additional arguments that will be passed to the event handler.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 86)
Returns:

true if the event had listeners, else false.

Type
boolean

eventNames()

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

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 55)
Returns:
Type
array

<protected> isLocked(sound, prop [, value])

Method used internally by Phaser.Sound.HTML5AudioSound class methods and property setters
to check if sound manager is locked and then either perform action immediately or queue it
to be performed once the sound manager gets unlocked.

Parameters:
Name Type Argument Description
sound Phaser.Sound.HTML5AudioSound

Sound object on which to perform queued action.

prop string

Name of the method to be called or property to be assigned a value to.

value * <optional>

An optional parameter that either holds an array of arguments to be passed to the method call or value to be set to the property.

Since: 3.0.0
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 326)
Returns:

Whether the sound manager is locked.

Type
boolean

listenerCount(event)

Return the number of listeners listening to a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 75)
Returns:

The number of listeners.

Type
number

listeners(event)

Return the listeners registered for a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 64)
Returns:

The registered listeners.

Type
array

off(event, fn, context, once)

Remove the listeners of a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

fn function

Only remove the listeners that match this function.

context *

Only remove the listeners that have this context.

once boolean

Only remove one-time listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 151)
Returns:

this.

Type
Phaser.Events.EventEmitter

on(event, fn [, context])

Add a listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 98)
Returns:

this.

Type
Phaser.Events.EventEmitter

<protected> onBlur()

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

Since: 3.0.0
Overrides:
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 273)

once(event, fn [, context])

Add a one-time listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 124)
Returns:

this.

Type
Phaser.Events.EventEmitter

<protected> onFocus()

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

Since: 3.0.0
Overrides:
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 293)

pauseAll()

Pauses all the sounds in the game.

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 367)
Fires:

play(key [, extra])

Enables playing sound on the fly without the need to keep a reference to it.
Sound will auto destroy once its playback ends.

Parameters:
Name Type Argument Description
key string

Asset key for the sound.

extra SoundConfig | SoundMarker <optional>

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

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 244)
Returns:

Whether the sound started playing successfully.

Type
boolean

playAudioSprite(key, spriteName [, config])

Enables playing audio sprite sound on the fly without the need to keep a reference to it.
Sound will auto destroy once its playback ends.

Parameters:
Name Type Argument Description
key string

Asset key for the sound.

spriteName string

The name of the sound sprite to play.

config SoundConfig <optional>

An optional config object containing default sound settings.

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 281)
Returns:

Whether the audio sprite sound started playing successfully.

Type
boolean

remove(sound)

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.

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 303)
Returns:

True if the sound was removed successfully, otherwise false.

Type
boolean

removeAllListeners( [event])

Remove all listeners, or those of the specified event.

Parameters:
Name Type Argument Description
event string | symbol <optional>

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 165)
Returns:

this.

Type
Phaser.Events.EventEmitter

removeByKey(key)

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.

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 330)
Returns:

The number of matching sound objects that were removed.

Type
number

removeListener(event, fn, context, once)

Remove the listeners of a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

fn function

Only remove the listeners that match this function.

context *

Only remove the listeners that have this context.

once boolean

Only remove one-time listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 137)
Returns:

this.

Type
Phaser.Events.EventEmitter

resumeAll()

Resumes all the sounds in the game.

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 389)
Fires:

setDetune(value)

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.

Since: 3.3.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 604)
Fires:
Returns:

This Sound Manager.

Type
Phaser.Sound.BaseSoundManager

setMute(value)

Sets the muted state of all this Sound Manager.

Parameters:
Name Type Description
value boolean

true to mute all sounds, false to unmute them.

Since: 3.3.0
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 363)
Fires:
Returns:

This Sound Manager.

Type
Phaser.Sound.HTML5AudioSoundManager

setRate(value)

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.

Since: 3.3.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 552)
Fires:
Returns:

This Sound Manager.

Type
Phaser.Sound.BaseSoundManager

setVolume(value)

Sets the volume of this Sound Manager.

Parameters:
Name Type Description
value number

The global volume of this Sound Manager.

Since: 3.3.0
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 414)
Fires:
Returns:

This Sound Manager.

Type
Phaser.Sound.HTML5AudioSoundManager

shutdown()

Removes all listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 31)

stopAll()

Stops all the sounds in the game.

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 411)
Fires:

unlock()

Unlocks HTML5 Audio loading and playback on mobile
devices on the initial explicit user interaction.

Since: 3.0.0
Overrides:
Source: src/sound/html5/HTML5AudioSoundManager.js (Line 148)

<protected> update(time, delta)

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.

Since: 3.0.0
Inherited From:
Source: src/sound/BaseSoundManager.js (Line 463)

Events


detune

Parameters:
Name Type Description
soundManager Phaser.Sound.BaseSoundManager

Reference to the sound manager that emitted event.

value number

An updated value of Phaser.Sound.BaseSoundManager#detune property.

Inherited From:
Source: src/sound/BaseSoundManager.js (Line 623)

muteEvent

Parameters:
Name Type Description
soundManager Phaser.Sound.HTML5AudioSoundManager

Reference to the sound manager that emitted event.

value boolean

An updated value of Phaser.Sound.HTML5AudioSoundManager#mute property.

Source: src/sound/html5/HTML5AudioSoundManager.js (Line 357)

pauseall

Parameters:
Name Type Description
soundManager Phaser.Sound.BaseSoundManager

Reference to the sound manager that emitted event.

Inherited From:
Source: src/sound/BaseSoundManager.js (Line 362)

rate

Parameters:
Name Type Description
soundManager Phaser.Sound.BaseSoundManager

Reference to the sound manager that emitted event.

value number

An updated value of Phaser.Sound.BaseSoundManager#rate property.

Inherited From:
Source: src/sound/BaseSoundManager.js (Line 546)

resumeall

Parameters:
Name Type Description
soundManager Phaser.Sound.BaseSoundManager

Reference to the sound manager that emitted event.

Inherited From:
Source: src/sound/BaseSoundManager.js (Line 384)

stopall

Parameters:
Name Type Description
soundManager Phaser.Sound.BaseSoundManager

Reference to the sound manager that emitted event.

Inherited From:
Source: src/sound/BaseSoundManager.js (Line 406)

unlocked

Parameters:
Name Type Description
soundManager Phaser.Sound.BaseSoundManager

Reference to the sound manager that emitted event.

Inherited From:
Source: src/sound/BaseSoundManager.js (Line 481)

volumeEvent

Parameters:
Name Type Description
soundManager Phaser.Sound.HTML5AudioSoundManager

Reference to the sound manager that emitted event.

value number

An updated value of Phaser.Sound.HTML5AudioSoundManager#volume property.

Source: src/sound/html5/HTML5AudioSoundManager.js (Line 408)