Skip to main content

Sound

The [[Sound]] object allows games built in Excalibur to load audio components, from soundtracks to sound effects. [[Sound]] is an [[Loadable]] which means it can be passed to a [[Loader]] to pre-load before a game or level.

Implements

Index

Constructors

constructor

  • new Sound(...paths: string[]): Sound
  • Parameters

    • rest...paths: string[]

      A list of audio sources (clip.wav, clip.mp3, clip.ogg) for this audio clip. This is done for browser compatibility.

    Returns Sound

Properties

publicdata

data: AudioBuffer

Data associated with a loadable

publicevents

events: EventEmitter<SoundEvents> = ...

publiclogger

logger: Logger = ...

Accessors

publicbustCache

  • get bustCache(): boolean
  • set bustCache(val: boolean): void
  • Should excalibur add a cache busting querystring? By default false. Must be set before loading


    Returns boolean

  • Parameters

    • val: boolean

    Returns void

publicduration

  • get duration(): number
  • set duration(duration: number): void
  • Get the duration that this audio should play. If unset the total natural playback duration will be used.


    Returns number

  • Set the duration that this audio should play. If unset the total natural playback duration will be used.

    Note: if you seek to a specific point the duration will start from that point, for example

    If you have a 10 second clip, seek to 5 seconds, then set the duration to 2, it will play the clip from 5-7 seconds.


    Parameters

    • duration: number

    Returns void

publicinstances

  • Return array of Current AudioInstances playing or being paused


    Returns Audio[]

publicloop

  • get loop(): boolean
  • set loop(value: boolean): void
  • Whether the audio should loop (repeat forever)


    Returns boolean

  • Indicates whether the clip should loop when complete


    Parameters

    • value: boolean

      Set the looping flag

    Returns void

publicpath

  • get path(): string
  • set path(val: string): void
  • Returns string

  • Parameters

    • val: string

    Returns void

publicplaybackRate

  • get playbackRate(): number
  • set playbackRate(playbackRate: number): void
  • Set the playbackRate, default is 1.0 at normal speed. For example 2.0 is double speed, and 0.5 is half speed.


    Returns number

  • Set the playbackRate, default is 1.0 at normal speed. For example 2.0 is double speed, and 0.5 is half speed.


    Parameters

    • playbackRate: number

    Returns void

publicvolume

  • get volume(): number
  • set volume(value: number): void
  • The volume (between 0 and 1)


    Returns number

  • The volume (between 0 and 1)


    Parameters

    • value: number

    Returns void

Methods

publicdecodeAudio

  • decodeAudio(data: ArrayBuffer): Promise<AudioBuffer>
  • Parameters

    • data: ArrayBuffer

    Returns Promise<AudioBuffer>

publicemit

  • emit<TEventName>(eventName: TEventName, event: SoundEvents[TEventName]): void
  • emit(eventName: string, event?: any): void

publicgetPlaybackPosition

  • getPlaybackPosition(trackId?: number): number
  • Return the current playback time of the playing track in seconds from the start.

    Optionally specify the track to query if multiple are playing at once.


    Parameters

    • trackId: number = 0

    Returns number

publicgetTotalPlaybackDuration

  • getTotalPlaybackDuration(): number
  • Return the duration of the sound


    Returns number

publicgetTrackId

  • getTrackId(track: Audio): number
  • Get Id of provided AudioInstance in current trackList


    Parameters

    • track: Audio

      [[Audio]] which Id is to be given

    Returns number

publicinstanceCount

  • instanceCount(): number
  • Returns how many instances of the sound are currently playing


    Returns number

publicisLoaded

  • isLoaded(): boolean
  • Returns true if the loadable is loaded


    Returns boolean

publicisPaused

  • isPaused(): boolean
  • Returns if the audio is paused


    Returns boolean

publicisPlaying

  • isPlaying(): boolean
  • Whether or not the sound is playing right now


    Returns boolean

publicisStopped

  • isStopped(): boolean
  • Returns if the audio is stopped


    Returns boolean

publicload

  • load(): Promise<AudioBuffer>
  • Begins loading the resource and returns a promise to be resolved on completion


    Returns Promise<AudioBuffer>

publicoff

  • off<TEventName>(eventName: TEventName, handler: Handler<SoundEvents[TEventName]>): void
  • off(eventName: string, handler: Handler<unknown>): void
  • off(eventName: string): void

publicon

publiconce

publicpause

  • pause(): void
  • Stop the sound, and do not rewind


    Returns void

publicplay

  • play(volume?: number): Promise<boolean>
  • Play the sound, returns a promise that resolves when the sound is done playing An optional volume argument can be passed in to play the sound. Max volume is 1.0


    Parameters

    • optionalvolume: number

    Returns Promise<boolean>

publicseek

  • seek(position: number, trackId?: number): void
  • Seek to a position (in seconds) in the audio


    Parameters

    • position: number
    • trackId: number = 0

    Returns void

publicstop

  • stop(): void
  • Stop the sound if it is currently playing and rewind the track. If the sound is not playing, rewinds the track.


    Returns void

publicwireEngine

  • wireEngine(engine: Engine<any>): void
  • Parameters

    Returns void