me.audio

There is no constructor function for me.audio.

Methods

static disable()

disable audio output

static enable()

enable audio output
only useful if audio supported and previously disabled through

See:
  • me.audio#disable

static fade(sound_name, from, to, duration, idopt)

Fade a currently playing sound between two volumee.

Parameters:
Name Type Attributes Description
sound_name String

audio clip name - case sensitive

from Number

Volume to fade from (0.0 to 1.0).

to Number

Volume to fade to (0.0 to 1.0).

duration Number

Time in milliseconds to fade.

id Number <optional>

the sound instance ID. If none is passed, all sounds in group will fade.

static getCurrentTrack() → {String}

returns the current track Id

Returns:
String -

audio track name

static getVolume() → {Number}

get the default global volume

Returns:
Number -

current volume value in Float [0.0 - 1.0] .

static init(audioFormatopt) → {Boolean}

Initialize and configure the audio support.
melonJS supports a wide array of audio codecs that have varying browser support : ("mp3", "mpeg", opus", "ogg", "oga", "wav", "aac", "caf", "m4a", "mp4", "weba", "webm", "dolby", "flac").
For a maximum browser coverage the recommendation is to use at least two of them, typically default to webm and then fallback to mp3 for the best balance of small filesize and high quality, webm has nearly full browser coverage with a great combination of compression and quality, and mp3 will fallback gracefully for other browsers. It is important to remember that melonJS selects the first compatible sound based on the list of extensions and given order passed here. So if you want webm to be used before mp3, you need to put the audio format in that order.

Parameters:
Name Type Attributes Default Description
audioFormat String <optional>
"mp3"

audio format provided

Returns:
Boolean -

Indicates whether audio initialization was successful

Example
// initialize the "sound engine", giving "webm" as default desired audio format, and "mp3" as a fallback
if (!me.audio.init("webm,mp3")) {
    alert("Sorry but your browser does not support html 5 audio !");
    return;
}

static mute(sound_name, idopt, muteopt)

mute or unmute the specified sound, but does not pause the playback.

Parameters:
Name Type Attributes Default Description
sound_name String

audio clip name - case sensitive

id Number <optional>

the sound instance ID. If none is passed, all sounds in group will mute.

mute Boolean <optional>
true

True to mute and false to unmute

Example
// mute the background music
me.audio.mute("awesome_music");

static muteAll()

mute all audio

static muted() → {Boolean}

Returns true if audio is muted globally.

Returns:
Boolean -

true if audio is muted globally

static pause(sound_name, idopt)

pause the specified sound on all channels
this function does not reset the currentTime property

Parameters:
Name Type Attributes Description
sound_name String

audio clip name - case sensitive

id Number <optional>

the sound instance ID. If none is passed, all sounds in group will pause.

Example
me.audio.pause("cling");

static pauseTrack()

pause the current audio track

Example
me.audio.pauseTrack();

static play(sound_name, loopopt, onendopt, volumeopt) → {Number}

play the specified sound

Parameters:
Name Type Attributes Default Description
sound_name String

audio clip name - case sensitive

loop Boolean <optional>
false

loop audio

onend function <optional>

Function to call when sound instance ends playing.

volume Number <optional>
default

Float specifying volume (0.0 - 1.0 values accepted).

Returns:
Number -

the sound instance ID.

Example
// play the "cling" audio clip
me.audio.play("cling");
// play & repeat the "engine" audio clip
me.audio.play("engine", true);
// play the "gameover_sfx" audio clip and call myFunc when finished
me.audio.play("gameover_sfx", false, myFunc);
// play the "gameover_sfx" audio clip with a lower volume level
me.audio.play("gameover_sfx", false, null, 0.5);

static playTrack(sound_name, volumeopt) → {Number}

play the specified audio track
this function automatically set the loop property to true
and keep track of the current sound being played.

Parameters:
Name Type Attributes Default Description
sound_name String

audio track name - case sensitive

volume Number <optional>
default

Float specifying volume (0.0 - 1.0 values accepted).

Returns:
Number -

the sound instance ID.

Example
me.audio.playTrack("awesome_music");

static rate(sound_name, rateopt, idopt)

get or set the rate of playback for a sound.

Parameters:
Name Type Attributes Description
sound_name String

audio clip name - case sensitive

rate Number <optional>

playback rate : 0.5 to 4.0, with 1.0 being normal speed.

id Number <optional>

the sound instance ID. If none is passed, all sounds in group will be changed.

Returns:
-

return the current playback rate (if no extra parameters were given)

Example
// get the playback rate of the background music
var rate = me.audio.rate("dst-gameforest");
// speed up the playback of the background music
me.audio.rate("dst-gameforest", 2.0);

static resume(sound_name, idopt)

resume the specified sound on all channels

Parameters:
Name Type Attributes Description
sound_name String

audio clip name - case sensitive

id Number <optional>

the sound instance ID. If none is passed, all sounds in group will resume.

Example
// play a audio clip
var id = me.audio.play("myClip");
...
// pause it
me.audio.pause("myClip", id);
...
// resume
me.audio.resume("myClip", id);

static resumeTrack()

resume the previously paused audio track

Example
// play an awesome music
me.audio.playTrack("awesome_music");
// pause the audio track
me.audio.pauseTrack();
// resume the music
me.audio.resumeTrack();

static seek(sound_name, seekopt, idopt)

get/set the position of playback for a sound.

Parameters:
Name Type Attributes Description
sound_name String

audio clip name - case sensitive

seek Number <optional>

The position to move current playback to (in seconds).

id Number <optional>

the sound instance ID. If none is passed, all sounds in group will changed.

Returns:
-

return the current seek position (if no extra parameters were given)

Example
// return the current position of the background music
var current_pos = me.audio.seek("dst-gameforest");
// set back the position of the background music to the beginning
me.audio.seek("dst-gameforest", 0);

static setVolume(volume)

set the default global volume

Parameters:
Name Type Description
volume Number

Float specifying volume (0.0 - 1.0 values accepted).

static stop(sound_name, idopt)

stop the specified sound on all channels

Parameters:
Name Type Attributes Description
sound_name String

audio clip name - case sensitive

id Number <optional>

the sound instance ID. If none is passed, all sounds in group will stop.

Example
me.audio.stop("cling");

static stopTrack()

stop the current audio track

See:
  • me.audio#playTrack
Example
// play a awesome music
me.audio.playTrack("awesome_music");
// stop the current music
me.audio.stopTrack();

static unload(sound_name) → {Boolean}

unload specified audio track to free memory

Parameters:
Name Type Description
sound_name String

audio track name - case sensitive

Returns:
Boolean -

true if unloaded

Example
me.audio.unload("awesome_music");

static unloadAll()

unload all audio to free memory

Example
me.audio.unloadAll();

static unmute(sound_name, idopt)

unmute the specified sound

Parameters:
Name Type Attributes Description
sound_name String

audio clip name

id Number <optional>

the sound instance ID. If none is passed, all sounds in group will unmute.

static unmuteAll()

unmute all audio