Options
All
  • Public
  • Public/Protected
  • All
Menu

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.

Pre-loading sounds

Pass the Sound to a Loader to pre-load the asset. Once a Sound is loaded, you can play it. You can pass an argument from 0.0 - 1.0 into play in order to play the sound at that volume.

// 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

Implements

Index

Constructors

constructor

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

Properties

arrayBuffer

arrayBuffer: ArrayBuffer = null

bustCache

bustCache: boolean

Whether or not to cache-bust requests

data

data: Blob | ArrayBuffer = null

eventDispatcher

eventDispatcher: EventDispatcher

Direct access to the game object event dispatcher.

logger

logger: Logger = Logger.getInstance()

path

path: string

Path to the remote resource

responseType

responseType: "" | "arraybuffer" | "blob" | "document" | "json" | "text"

The type to expect as a response: "" | "arraybuffer" | "blob" | "document" | "json" | "text";

Accessors

duration

  • get duration(): number | undefined

instances

loop

  • get loop(): boolean
  • set loop(value: boolean): void

volume

  • get volume(): number
  • set volume(value: number): void

Methods

emit

  • emit(eventName: string, eventObject: GameEvent<any>): void

getArrayData

  • getArrayData(): any

getData

  • getData(): any

getTrackId

instanceCount

  • instanceCount(): number

isLoaded

  • isLoaded(): boolean

isPlaying

  • isPlaying(): boolean

load

  • load(): Promise<Blob | ArrayBuffer>

off

  • off(eventName: string, handler?: function): void
  • Alias for removeEventListener. If only the eventName is specified it will remove all handlers registered for that specific event. If the eventName and the handler instance are specified only that handler will be removed.

    Parameters

    • eventName: string

      Name of the event to listen for

    • Optional handler: function

      Event handler for the thrown event

    Returns void

on

  • on(eventName: string, handler: function): void
  • Alias for addEventListener. You can listen for a variety of events off of the engine; see the events section below for a complete list.

    Parameters

    • eventName: string

      Name of the event to listen for

    • handler: function

      Event handler for the thrown event

    Returns void

once

  • once(eventName: string, handler: function): void
  • Once listens to an event one time, then unsubscribes from that event

    Parameters

    • eventName: string

      The name of the event to subscribe to once

    • handler: function

      The handler of the event that will be auto unsubscribed

    Returns void

oncomplete

  • oncomplete(): void

onerror

  • onerror(): void

onprogress

  • onprogress(): void

pause

  • pause(): void

play

  • 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

    • Optional volume: number

    Returns Promise<boolean>

processData

  • processData(data: Blob | ArrayBuffer): Promise<string | AudioBuffer>

setData

  • setData(data: any): void

stop

  • stop(): void

wireEngine

  • wireEngine(engine: Engine): void