waud.js
API Docs for v1.0.3
Show:

WaudSound

Defined in: src/WaudSound.hx:60

Summary

Class to automatically use web audio api with HTML5 audio fallback.

Constructor

WaudSound

Defined in src/WaudSound.hx:60

Syntax

WaudSound

(
  • url
  • [options]
)

Summary

Parameters:

  • url String
    • Can be audio file path or JSON file for audio sprite.
  • [options] WaudSoundOptions optional
    • Sound options.

Example:

    // MP3 Sound
    var snd = new WaudSound("assets/loop.mp3", { autoplay: false, loop: true, volume: 0.5 });

    // Force HTML5 Audio
    var snd = new WaudSound("assets/loop.mp3", { webaudio: false });

    // Data URI
    // Note that the data URI used below is a sample string and not a valid sound
    var base64Snd = new WaudSound("data:audio/mpeg;base64,//uQxAAAAAAAAAAAAASW5mbwAAAA8AAABEAABwpgADBwsLDxISF");
    base64Snd.play();

    // Audio Sprite
    var audSprite = new WaudSound("assets/sprite.json");
    audSprite.play("glass");

Methods

_init

Syntax

_init

(
  • url
)
private

Summary

Function to initialize sound.

Parameters:

_loadSpriteJson

Syntax

_loadSpriteJson

(
  • url
)
private

Summary

Function to load audio sprite JSON.

Parameters:

  • url String
    • Audio Sprite JSON path.

autoStop

Syntax

autoStop

(
  • val
)

Summary

Function to automatically stop the sound if it's already playing or play the sound in a different channel.

By default, sound will automatically stop if play is called on the sound that is already playing.

Parameters:

  • val Bool

Example:

snd.autoStop(false);

destroy

Syntax

destroy

()

Summary

Function to destroy sound.

Example:

snd.destroy();

getDuration

Syntax

getDuration

() Float

Summary

Function to get sound duration.

Returns:

Float:

sound duration

Example:

snd.getDuration();

getTime

Syntax

getTime

()

Summary

Function to get the current playback position in seconds.

Not supported for audio sprites yet.

Example:

snd.getTime();

getVolume

Syntax

getVolume

() Float

Summary

Function to get sound volume.

Returns:

Float:

between 0 and 1

Example:

snd.getVolume();

isPlaying

Syntax

isPlaying

(
  • [spriteName]
)
Bool

Summary

Function to check if the sound is playing or not.

Parameters:

  • [spriteName] String optional
    • Sprite name

Returns:

Bool:

true or false

Example:

snd.isPlaying();

isReady

Syntax

isReady

() Bool

Summary

Function to check if the sound is ready to be played.

Returns:

Bool:

true or false

Example:

snd.isReady();

load

Syntax

load

(
  • [callback]
)
IWaudSound

Summary

Function to manually load the sound if preload was set to false with optional onload callback.

Parameters:

  • [callback] Function optional
    • onload callback function.

Returns:

IWaudSound:

sound instance

Example:

snd.load();
snd.load(callback);

loop

Syntax

loop

(
  • val
)

Summary

Function to loop or unloop sound.

Parameters:

  • val Bool

Example:

snd.loop(true);

mute

Syntax

mute

(
  • val
)

Summary

Function to mute sound.

Parameters:

  • val Bool

Example:

snd.mute(true);

onEnd

Syntax

onEnd

(
  • callback
)
IWaudSound

Summary

Function to add callback that triggers when the sound finishes playing.

Parameters:

Returns:

IWaudSound:

sound instance

Example:

snd.onEnd(callback);

onError

Syntax

onError

(
  • callback
)
IWaudSound

Summary

Function to add callback that triggers when the sound fails to load or if it fails to decode when using web audio.

Parameters:

Returns:

IWaudSound:

sound instance

Example:

snd.onError(callback);

onLoad

Syntax

onLoad

(
  • callback
)
IWaudSound

Summary

Function to add callback that triggers when the sound is loaded.

Parameters:

Returns:

IWaudSound:

sound instance

Example:

snd.onLoad(callback);

pause

Syntax

pause

(
  • [spriteName]
)

Summary

Function to pause sound.

Parameters:

  • [spriteName] String optional
    • Sprite name to pause.

Example:

snd.pause();

play

Syntax

play

(
  • [spriteName]
)
Int

Summary

Function to play the sound with optional sprite name when using audio sprite.

Parameters:

  • [spriteName] String optional
    • Sprite name to play.

Returns:

Int:

sound id

Example:

snd.play();
snd.play("bell");

playbackRate

Syntax

playbackRate

(
  • [val]
)
Float

Summary

Function to set playback rate.

Parameters:

  • [val] Float optional
    • playback rate.

Returns:

Float:

current playback rate.

Example:

snd.playbackRate(1.25);

setTime

Syntax

setTime

(
  • time
)

Summary

Function to set playback position in seconds.

Not supported for audio sprites yet.

Parameters:

  • time Float
    • playback position in seconds.

Example:

snd.setTime(30);

setVolume

Syntax

setVolume

(
  • val
)

Summary

Function to set sound volume.

Parameters:

  • val Float
    • Should be between 0 and 1.

Example:

snd.setVolume(0.5);

stop

Syntax

stop

(
  • [spriteName]
)

Summary

Function to stop sound.

Parameters:

  • [spriteName] String optional
    • Sprite name

Example:

snd.stop();

toggleMute

Syntax

toggleMute

()

Summary

Function to automatically mute or unmute the sound.

Example:

snd.toggleMute();

togglePlay

Syntax

togglePlay

(
  • [spriteName]
)

Summary

Function to automatically pause or play the sound.

Parameters:

  • [spriteName] String optional
    • Sprite name to toggle play.

Example:

snd.togglePlay();

Properties

isSpriteSound

Defined in src/WaudSound.hx:9

Syntax

isSpriteSound

Bool

Summary

Indicates if the sound is sprite sound or normal sound.

Example:

snd.isSpriteSound;

rate

Defined in src/WaudSound.hx:42

Syntax

rate

Float

Summary

Sound playback rate.

Example:

snd.rate;

spriteName

Defined in src/WaudSound.hx:31

Syntax

spriteName

String

Summary

Sound sprite name.

Example:

snd.spriteName;

url

Defined in src/WaudSound.hx:20

Syntax

url

String

Summary

Sound url.

Example:

snd.url;