Sound

Creates a sound that can be played

Constructor

new Sound(path)

Parameters:
NameTypeDescription
pathString

The path to the sound file

Example
let mySound = new gameify.audio.Sound("my-sound.mp3");

Members

(readonly) loaded

If the sound has loaded yet

(readonly) path

The sound path. Modifying this will not do anything.

Methods

changePath(path)

Change and load a new image path. Resets the image's crop

Parameters:
NameTypeDescription
pathstring

The new image path

fromJSON(data, ref) → {gameify.Image}

Creates a object from JSON data

Parameters:
NameTypeDescription
dataObject | Array

Serialized object data (from object.toJSON)

reffunction

A function that returns a name for other objects, so they can be restored later

Returns:
Type: 
gameify.Image

getCalculatedVolume() → {Number}

Get the calculated volume of the sound (after audioManager volume is applied)

Returns:

The calculated volume of the sound, between 0 and 1

Type: 
Number

getCurrentTime() → {Number}

Get the current seek time of the sound

Returns:

The current seek time, in seconds

Type: 
Number

getDuration() → {Number}

Get the duration of the audio (in seconds)

Returns:

The duration of the sound in seconds

Type: 
Number

getLoop() → {Boolean}

Check if the sound is set to loop

Returns:

If the sound is set to loop

Type: 
Boolean

getVolume() → {Number}

Get the volume of the sound. Note that this volume is mixed with the audioManager's volume to get the actual volume. Use getCalculatedVolume() to get the actual volume the sound will be played at.

See
  • {gameify.audio.Sound.getCalculatedVolume}
Returns:

The volume of the sound, between 0 and 1

Type: 
Number

isPlaying() → {Boolean}

Check if the sound currently is playing

Returns:
Type: 
Boolean

onLoad(callback)

Set a function to be run when the sound is loaded (Can be played through w/o buffering)

Parameters:
NameTypeDescription
callbackfunction

The function to be called when the sound is loaded.

pause()

Pause the sound

play()

Play the sound. Please be aware some browsers block autoplay by default.

seek(time)

Seek to a specific point in the audio

Parameters:
NameTypeDescription
timeNumber

The time to seek to, in seconds

seekRelative(time)

Seek a percentage of the way through the audio

Parameters:
NameTypeDescription
timeNumber

Where to seek to, from 0 to 1

setLoop(loop)

Choose whether the sound should loop or not (default is not looping)

Parameters:
NameTypeDescription
loopBoolean

If the sound should loop

setVolume(volume)

Sets the volume of the sound

Parameters:
NameTypeDescription
volumeNumber

The desired volume level, a number between 0 and 1.

stop()

Stop the sound (resets the seek time)

toJSON(keyopt, ref) → {Object}

Convert the object to JSON

Parameters:
NameTypeAttributesDescription
keystring<optional>

Key object is stored under (unused, here for consistency with e.g. Date.toJSON, etc.)

reffunction

A function that returns a name for other objects, so they can be restored later

Returns:
Type: 
Object