Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Sound

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

Pre-loading sounds

Pass the Sound to a Loader to pre-load the asset. Once a Sound is loaded, you can play it.

// define multiple sources (such as mp3/wav/ogg) as a browser fallback
var sndPlayerDeath = new ex.Sound("/assets/snd/player-death.mp3", "/assets/snd/player-death.wav");
var loader = new ex.Loader(sndPlayerDeath);
game.start(loader).then(function () {
  sndPlayerDeath.play();
});

Hierarchy

  • Sound

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

Private _data

_data: any = null

Private _engine

_engine: Engine

Private _isLoaded

_isLoaded: boolean = false

Private _isPaused

_isPaused: boolean = false

Private _logger

_logger: Logger = Logger.getInstance()

Private _loop

_loop: boolean = false

Private _tracks

_tracks: IAudio[] = []

Private _volume

_volume: number = 1

Private _wasPlayingOnHidden

_wasPlayingOnHidden: boolean = false

path

path: string

sound

Populated once loading is complete

Methods

Private _fetchResource

  • _fetchResource(onload: function): void
  • Parameters

    • onload: function
        • (XMLHttpRequest: any): void
        • Parameters

          • XMLHttpRequest: any

          Returns void

    Returns void

getData

  • getData(): any
  • Gets the raw sound data (e.g. blob URL or AudioBuffer)

    Returns any

instanceCount

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

    Returns number

isLoaded

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

    Returns boolean

isPlaying

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

    Returns boolean

load

oncomplete

  • oncomplete(): void
  • Returns void

onerror

  • onerror(): void
  • Returns void

onprogress

  • onprogress(): void
  • Returns void

pause

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

    Returns void

play

  • Play the sound, returns a promise that resolves when the sound is done playing

    Returns Promise<boolean>

processData

  • processData(data: any): any
  • Set the raw sound data (e.g. blob URL or AudioBuffer)

    Parameters

    • data: any

    Returns any

setData

  • Sets raw sound data and returns a Promise that is resolved when sound data is processed

    Parameters

    • data: any

      The XHR data for the sound implementation to process (Blob or ArrayBuffer)

    Returns Promise<any>

setLoop

  • setLoop(loop: boolean): void
  • Indicates whether the clip should loop when complete

    Parameters

    • loop: boolean

      Set the looping flag

    Returns void

setVolume

  • setVolume(volume: number): void
  • Sets the volume of the sound clip

    Parameters

    • volume: number

      A volume value between 0-1.0

    Returns void

stop

  • stop(): void
  • Stop the sound and rewind

    Returns void

wireEngine

  • wireEngine(engine: Engine): void

Static canPlayFile

  • canPlayFile(file: string): boolean
  • Whether or not the browser can play this file as HTML5 Audio

    Parameters

    • file: string

    Returns boolean