• Public
  • Public/Protected
  • All

An AudioSourceNode container which handles the strategy of node type to use for playback. Used by the Sound interface which controls playback. This class is for internal use only and should not be used by external callers.


  • AudioContainer




src: string

The audio source path

sourceNode: AudioBufferSourceNode | MediaElementAudioSourceNode = undefined

The Audio Node used to control this sound

gainNode: GainNode = undefined

The GainNode used to control volume

isBuffer: boolean = false

Is this container using an AudioBuffer?

loadState: number = AudioContainer.LOAD_STATES.NONE

Whether we have attempted to load the audio node or not, and whether it failed.



playing: boolean = false

Is the audio source currently playing?

_loop: boolean = false

Should the audio source loop?


The maximum duration, in seconds, for which an AudioBuffer will be used. Otherwise, a streaming media element will be used.

LOAD_STATES: { FAILED: number; NONE: number; LOADING: number; LOADED: number } = ...

The sequence of container loading states.

Type declaration

  • FAILED: number
  • NONE: number
  • LOADING: number
  • LOADED: number


  • get loop(): boolean
  • set loop(looping: boolean): void
  • Returns boolean

  • Parameters

    • looping: boolean

    Returns void

  • get loaded(): boolean
  • Has the audio file been loaded either fully or for streaming.

    Returns boolean

  • get failed(): boolean
  • Did the audio file fail to load.

    Returns boolean

  • get buffer(): AudioBuffer
  • A reference to the AudioBuffer if the sourceNode is a AudioBufferSourceNode.

    Returns AudioBuffer

  • get context(): AudioContext
  • The game audio context used throughout the application.

    Returns AudioContext

  • get duration(): number
  • The total duration of the audio source in seconds

    Returns number

  • get element(): HTMLMediaElement
  • A reference to the HTMLMediaElement, if the sourceNode is a MediaElementAudioSourceNode.

    Returns HTMLMediaElement


  • load(): Promise<void>
  • Load the source node required for playback of this audio source

    Returns Promise<void>

  • createAudioBuffer(src: string): Promise<AudioBuffer>
  • Create an Audio source node using a buffered array.


    • src: string

      The source URL from which to create the buffer

    Returns Promise<AudioBuffer>

    The created and decoded buffer

  • play(offset?: number, onended?: Function): void
  • Begin playback for the source node.


    • offset: number = 0
    • onended: Function = undefined

    Returns void

  • stop(): void
  • Terminate playback for the source node.

    Returns void

  • _createNode(): AudioBufferSourceNode | MediaElementAudioSourceNode
  • Create the initial audio node used for playback. Determine the node type to use based on cached state and sound duration.

    Returns AudioBufferSourceNode | MediaElementAudioSourceNode

  • _createAudioBufferSourceNode(buffer: any): AudioBufferSourceNode
  • Create a AudioBufferSourceNode using a provided AudioBuffer


    • buffer: any

    Returns AudioBufferSourceNode

  • _createAudioElement(): Promise<HTMLAudioElement>
  • Create an HTML5 Audio element which has loaded the metadata for the provided source.

    Returns Promise<HTMLAudioElement>

  • _createMediaElementAudioSourceNode(element: any): MediaElementAudioSourceNode
  • Create a MediaElementAudioSourceNode using a provided HTMLAudioElement


    • element: any

    Returns MediaElementAudioSourceNode

  • _onEnd(onended: Function): void
  • Perform cleanup actions when the sound has finished playing. For MediaElementAudioSourceNodes, this also means optionally restarting if the sound is supposed to loop.


    • onended: Function

      A callback provided by the owner of the container that gets fired when the sound ends.

    Returns void

  • _unloadMediaNode(): void
  • Unload the MediaElementAudioSourceNode to terminate any ongoing connections.

    Returns void