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

Get the duration of the audio in seconds.

filters Array<filters.Filter>

Set the filters. Only supported with WebAudio.

instances Array<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

If the current sound is playable (loaded).

isPlaying boolean readonly

true if the sound is currently being played.

Default Value:
  • false

loop

Gets and sets the looping.

media IMedia

The audio source

muted

Gets and sets the muted flag.

options Object readonly

The constructor options.

paused

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

The playback rate

sprites Object

Get the map of sprites.

url String readonly

The file source to load.

volume

Gets and sets the volume.

Methods

Sound.from (options) Sound static

Create a new sound instance from source.

Name Type Description
options ArrayBuffer | String | Options | HTMLAudioElement

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 object

Either completed function or play options.

data.start number

Time when to play the sound in seconds.

data.end number

Time to end playing in seconds.

data.speed number

Override default speed, default to the Sound's speed setting.

Returns:
Type Description
SoundSprite Sound sprite result.

addSprites (data) SoundSprites

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

Name Type Description
data Object

Map of sounds to add where the key is the alias, and the data are configuration options, see Sound#addSprite for info on data.

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 Instance of this sound.

play (alias, callback) 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 | Promise The sound instance, this cannot be reused after it is done playing. Returns a Promise if the sound has not yet loaded.

play (options) IMediaInstance | Promise

Plays the sound.

Name Type Attributes Description
options Function | PlayOptions <optional>

Either completed function or play options.

Returns:
Type Description
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 Sound instance for chaining.

resume () this

Resuming all the instances of this sound from playing

Returns:
Type Description
this Instance of this sound.

stop () this

Stops all the instances of this sound from playing.

Returns:
Type Description
this Instance of this sound.