PokéRogue
    Preparing search index...

    Class WebAudioSoundManager

    The Web Audio API implementation of the Phaser Sound Manager.

    This is the default Sound Manager used in Phaser when the browser supports the Web Audio API. It creates and manages an AudioContext, routes all sounds through a master gain node chain for global mute and volume control, and handles the browser autoplay policy by unlocking audio on the first user interaction.

    Use this manager to add, play, and control sounds in your game. It is accessed via this.sound from within a Scene. If the browser does not support the Web Audio API, Phaser will fall back to the HTML5AudioSoundManager instead.

    Not all browsers can play all audio formats.

    There is a good guide to what's supported: Cross-browser audio basics: Audio codec support.

    Hierarchy (View Summary)

    Index
    context: AudioContext

    The AudioContext being used for playback.

    destination: AudioNode

    Destination node for connecting individual sounds to.

    detune: number

    Global detuning of all sounds in cents. The range of the value is -1200 to 1200, but we recommend keeping it within a reasonable range for musical purposes.

    game: Game

    Local reference to game.

    gameLostFocus: boolean

    Flag used to track if the game has lost focus.

    jsonCache: BaseCache

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

    listenerPosition: Vector2

    The Spatial Audio listener position.

    Only available with WebAudio.

    You can modify the x/y properties of this Vec2 directly to adjust the listener position within the game world.

    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.

    masterMuteNode: GainNode

    Gain node responsible for controlling global muting.

    masterVolumeNode: GainNode

    Gain node responsible for controlling global volume.

    mute: boolean

    Global mute setting.

    pauseOnBlur: boolean

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

    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.

    volume: number

    Global volume setting.

    • Adds a new sound into the sound manager.

      Parameters

      • key: string

        Asset key for the sound.

      • Optionalconfig: SoundConfig

        An optional config object containing default sound settings.

      Returns WebAudioSound

      The new sound instance.

    • Add a listener for a given event.

      Parameters

      • event: string | symbol

        The event name.

      • fn: Function

        The listener function.

      • Optionalcontext: any

        The context to invoke the listener with. Default this.

      Returns this

      this.

    • Method responsible for instantiating and returning AudioContext instance. If an instance of an AudioContext class was provided through the game config, that instance will be returned instead. This can come in handy if you are reloading a Phaser game on a page that never properly refreshes (such as in an SPA project) and you want to reuse already instantiated AudioContext.

      Parameters

      • game: Game

        Reference to the current game instance.

      Returns AudioContext

      The AudioContext instance to be used for playback.

    • Decode audio data into a format ready for playback via Web Audio.

      The audio data can be a base64 encoded string, an audio media-type data uri, or an ArrayBuffer instance.

      The audioKey is the key that will be used to save the decoded audio to the audio cache.

      Instead of passing a single entry you can instead pass an array of Phaser.Types.Sound.DecodeAudioConfig objects as the first and only argument.

      Decoding is an async process, so be sure to listen for the events to know when decoding has completed.

      Once the audio has decoded it can be added to the Sound Manager or played via its key.

      Parameters

      • OptionalaudioKey: string | DecodeAudioConfig[]

        The string-based key to be used to reference the decoded audio in the audio cache, or an array of audio config objects.

      • OptionalaudioData: string | ArrayBuffer

        The audio data, either a base64 encoded string, an audio media-type data uri, or an ArrayBuffer instance.

      Returns void

    • Calls Phaser.Sound.BaseSoundManager#destroy method and cleans up all Web Audio API related resources.

      Returns void

    • Calls each of the listeners registered for a given event.

      Parameters

      • event: string | symbol

        The event name.

      • ...args: any[]

        Additional arguments that will be passed to the event handler.

      Returns boolean

      true if the event had listeners, else false.

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

      Returns (string | symbol)[]

      undefined

    • Gets the first sound in this Sound Manager that matches the given key. If none can be found it returns null.

      Type Parameters

      Parameters

      • key: string

        Sound asset key.

      Returns T

      • The sound, or null.
    • Gets all sounds in this Sound Manager.

      You can optionally specify a key, in which case only Sound instances that match the given key will be returned.

      Type Parameters

      Parameters

      • Optionalkey: string

        Optional asset key. If given, only Sound instances with this key will be returned.

      Returns T[]

      • The sounds, or an empty array.
    • Returns all sounds from this Sound Manager that are currently playing. That is, Sound instances that have their isPlaying property set to true.undefined

      Type Parameters

      Returns T[]

      • All currently playing sounds, or an empty array.
    • When a key is given, returns true if any sound with that key is playing.

      When no key is given, returns true if any sound is playing.

      Parameters

      • key: string | undefined

        Sound asset key.

      Returns boolean

      • Per the key argument, true if any matching sound is playing, otherwise false.
    • Return the number of listeners listening to a given event.

      Parameters

      • event: string | symbol

        The event name.

      Returns number

      The number of listeners.

    • Return the listeners registered for a given event.

      Parameters

      • event: string | symbol

        The event name.

      Returns Function[]

      The registered listeners.

    • Remove the listeners of a given event.

      Parameters

      • event: string | symbol

        The event name.

      • Optionalfn: Function

        Only remove the listeners that match this function.

      • Optionalcontext: any

        Only remove the listeners that have this context.

      • Optionalonce: boolean

        Only remove one-time listeners.

      Returns this

      this.

    • Add a listener for a given event.

      Parameters

      • event: string | symbol

        The event name.

      • fn: Function

        The listener function.

      • Optionalcontext: any

        The context to invoke the listener with. Default this.

      Returns this

      this.

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

      Returns void

    • Add a one-time listener for a given event. The listener is automatically removed the first time the event is emitted, so it will never be called more than once.

      Parameters

      • event: string | symbol

        The event name.

      • fn: Function

        The listener function.

      • Optionalcontext: any

        The context to invoke the listener with. Default this.

      Returns this

      this.

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

      Returns void

    • Pauses all the sounds in the game.

      Returns void

    • 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

      • key: string

        Asset key for the sound.

      • Optionalextra: SoundConfig | SoundMarker

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

      Returns boolean

      Whether the sound started playing successfully.

    • 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

      • key: string

        Asset key for the sound.

      • spriteName: string

        The name of the sound sprite to play.

      • Optionalconfig: SoundConfig

        An optional config object containing default sound settings.

      Returns boolean

      Whether the audio sprite sound started playing successfully.

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

      Parameters

      • sound: BaseSound

        The sound object to remove.

      Returns boolean

      True if the sound was removed successfully, otherwise false.

    • Removes all sounds from the manager, destroying the sounds.

      Returns void

    • Remove all listeners, or those of the specified event.

      Parameters

      • Optionalevent: string | symbol

        The event name.

      Returns this

      this.

    • Removes all sounds from the sound manager that have an asset key matching the given value. The removed sounds are destroyed before removal.

      Parameters

      • key: string

        The key to match when removing sound objects.

      Returns number

      The number of matching sound objects that were removed.

    • Remove the listeners of a given event.

      Parameters

      • event: string | symbol

        The event name.

      • Optionalfn: Function

        Only remove the listeners that match this function.

      • Optionalcontext: any

        Only remove the listeners that have this context.

      • Optionalonce: boolean

        Only remove one-time listeners.

      Returns this

      this.

    • Resumes all the sounds in the game.

      Returns void

    • This method takes a new AudioContext reference and then sets this Sound Manager to use that context for all playback.

      As part of this call it also disconnects the master mute and volume nodes and then re-creates them on the new given context.

      Parameters

      • context: AudioContext

        Reference to an already created AudioContext instance.

      Returns this

      The WebAudioSoundManager instance.

    • Sets the global detuning of all sounds in cents. The range of the value is -1200 to 1200, but we recommend keeping it within a reasonable range for musical purposes.

      Parameters

      • value: number

        The detuning value in cents. The range is -1200 to 1200, but we recommend keeping it within a reasonable range for musical purposes.

      Returns this

      This Sound Manager.

    • Sets the X and Y position of the Spatial Audio listener on this Web Audio context.

      If you call this method with no parameters it will default to the center-point of the game canvas. Depending on the type of game you're making, you may need to call this method constantly to reset the listener position as the camera scrolls.

      Calling this method does nothing on HTML5Audio.

      Parameters

      • Optionalx: number

        The x position of the Spatial Audio listener.

      • Optionaly: number

        The y position of the Spatial Audio listener.

      Returns void

    • Sets the muted state of this Sound Manager.

      Parameters

      • value: boolean

        true to mute all sounds, false to unmute them.

      Returns WebAudioSoundManager

      This Sound Manager.

    • 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

      • value: number

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

      Returns this

      This Sound Manager.

    • Sets the volume of this Sound Manager.

      Parameters

      • value: number

        The global volume of this Sound Manager.

      Returns WebAudioSoundManager

      This Sound Manager.

    • Removes all listeners from this EventEmitter. This method is called automatically by the Scene Systems when the parent Scene shuts down, ensuring that all event bindings are cleared and no stale references remain.

      Returns void

    • Stops all the sounds in the game.

      Returns void

    • Stops any sounds matching the given key.

      Parameters

      • key: string

        Sound asset key.

      Returns number

      • How many sounds were stopped.
    • Unlocks Web Audio API on the initial input event.

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

      Returns void

    • Update method called on every game step.

      Removes destroyed sounds and updates every active sound in the game.

      Parameters

      • time: number

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

      • delta: number

        The delta time elapsed since the last frame.

      Returns void