Class: Sound

Sound

Sound represents a single piece of loaded media. When playing a sound IMediaInstance objects are created. Properties such a volume, pause, mute, speed, etc will have an effect on all instances.

Members

autoPlay boolean readonly

true to start playing immediate after load.

Default Value:
  • false

context IMediaContext

Instance of the media context.

duration number

Get the duration of the audio in seconds.

filters filters.Filter[]

Set the filters. Only supported with WebAudio.

instances IMediaInstance[]

Gets the list of instances that are currently being played of this sound.

isLoaded boolean

true if the buffer is loaded.

Default Value:
  • false

isPlayable boolean

If the current sound is playable (loaded).

isPlaying boolean readonly

true if the sound is currently being played.

Default Value:
  • false

loop boolean

Gets and sets the looping.

media IMedia

The audio source

muted boolean

Gets and sets the muted flag.

options Options readonly

The constructor options.

paused boolean

Stops all the instances of this sound from playing.

preload boolean readonly

true to immediately start preloading.

Default Value:
  • false

singleInstance boolean

true to disallow playing multiple layered instances at once.

Default Value:
  • false

speed number

The playback rate.

sprites SoundSprites

Get the map of sprites.

url string readonly

The file source to load.

volume number

Gets and sets the volume.

Methods

Sound.from (source) Sound static

Create a new sound instance from source.

Name Type Description
source string | Options | ArrayBuffer | HTMLAudioElement | AudioBuffer

Either the path or url to the source file. or the object of options to use.

Returns:
Type Description
Sound Created sound instance.

addSprites (alias, data) SoundSprite

Add a sound sprite, which is a saved instance of a longer sound. Similar to an image spritesheet.

Name Type Description
alias string

The unique name of the sound sprite.

data SoundSpriteData

Either completed function or play options.

Returns:
Type Description
SoundSprite

addSprites (data) SoundSprites

Convenience method to add more than one sprite add a time.

Name Type Description
data SoundSpriteDataMap

Map of sounds to add where the key is the alias, and the data are configuration options.

Returns:
Type Description
SoundSprites The map of sound sprites added.

autoPlayStart () IMediaInstance

Auto play the first instance.

Returns:
Type Description
IMediaInstance

destroy () void

Destructor, safer to use SoundLibrary.remove(alias) to remove this sound.

pause () this

Stops all the instances of this sound from playing.

Returns:
Type Description
this

play (alias, callback) IMediaInstance | IMediaInstance<Promise>

Play a sound sprite, which is a saved instance of a longer sound. Similar to an image spritesheet.

Name Type Attributes Description
alias string | PlayOptions | CompleteCallback <optional>

The unique name of the sound sprite.

callback Function <optional>

Callback when completed.

Returns:
Type Description
IMediaInstance | IMediaInstance<Promise> The sound instance, this cannot be reused after it is done playing. Returns a Promise if the sound has not yet loaded.

play (source, callback) IMediaInstance | IMediaInstance<Promise>

Plays the sound.

Name Type Attributes Description
source Function | PlayOptions <optional>

Either completed function or play options.

callback Function <optional>

Callback when completed.

Returns:
Type Description
IMediaInstance | IMediaInstance<Promise> The sound instance, this cannot be reused after it is done playing. Returns a Promise if the sound has not yet loaded.

refresh () void

Internal only, speed, loop, volume change occured.

refreshPaused () void

Handle changes in paused state. Internal only.

removeSprites (alias) Sound

Remove a sound sprite.

Name Type Attributes Description
alias string <optional>

The unique name of the sound sprite, if alias is omitted, removes all sprites.

Returns:
Type Description
Sound

resume () this

Resuming all the instances of this sound from playing

Returns:
Type Description
this

stop () this

Stops all the instances of this sound from playing.

Returns:
Type Description
this