Crafty.audio

Add sound files and play them. Chooses best format for browser support. Due to the nature of HTML5 audio, three types of audio files will be required for cross-browser capabilities. These formats are MP3, Ogg and WAV. When sound was not muted on before pause, sound will be unmuted after unpause. When sound is muted Crafty.pause() does not have any effect on sound

The maximum number of sounds that can be played simultaneously is defined by Crafty.audio.maxChannels. The default value is 7.

Methods

Back to top

Crafty.audio.add()

public this Crafty.audio.add(String id, String url)
id

A string to refer to sounds

url

A string pointing to the sound file

public this Crafty.audio.add(String id, Array urls)
urls

Array of urls pointing to different format of the same sound, selecting the first that is playable

public this Crafty.audio.add(Object map)
map

key-value pairs where the key is the id and the value is either a url or urls

Loads a sound to be played. Due to the nature of HTML5 audio, three types of audio files will be required for cross-browser capabilities. These formats are MP3, Ogg and WAV.

Passing an array of URLs will determine which format the browser can play and select it over any other.

Accepts an object where the key is the audio name and either a URL or an Array of URLs (to determine which type to use).

The ID you use will be how you refer to that sound when using Crafty.audio.play.

Example

//adding audio from an object
Crafty.audio.add({
  shoot: ["sounds/shoot.wav",
          "sounds/shoot.mp3",
          "sounds/shoot.ogg"]
});

//adding a single sound
Crafty.audio.add("walk", [
"sounds/walk.mp3",
"sounds/walk.ogg",
"sounds/walk.wav"
]);

//only one format
Crafty.audio.add("jump", "sounds/jump.mp3");
Back to top

Crafty.audio.create()

public this Crafty.audio.create(String id, String url)
id

A string to refer to sounds

url

A string pointing to the sound file

Creates an audio asset with the given id and resource. Crafty.audio.add is a more flexible interface that allows cross-browser compatibility.

If the sound file extension is not supported, returns false; otherwise, returns the audio asset.

Back to top

Crafty.audio.isPlaying()

public Boolean Crafty.audio.isPlaying(string ID)
{string} id

The id of the audio object

[Returns]

a Boolean indicating whether the audio is playing or not

Check if audio with the given ID is playing or not (on at least one channel).

Example

var isPlaying = Crafty.audio.isPlaying('music');
Back to top

Crafty.audio.mute()

public this Crafty.audio.mute()

Mute every Audio instance that is playing.

Example

Crafty.audio.mute();
Back to top

Crafty.audio.pause()

public this Crafty.audio.pause(string ID)
{string} id

The id of the audio object to pause

Pause the Audio instance specified by id param.

Example

Crafty.audio.pause('music');
Back to top

Crafty.audio.play()

public this Crafty.audio.play(String id)
public this Crafty.audio.play(String id, Number repeatCount)
public this Crafty.audio.play(String id, Number repeatCount, Number volume)
id

A string to refer to sounds

repeatCount

Repeat count for the file, where -1 stands for repeat forever.

volume

volume can be a number between 0.0 and 1.0

[Returns]

The audio element used to play the sound. Null if the call failed due to a lack of open channels.

Will play a sound previously added by using the ID that was used in Crafty.audio.add. Has a default maximum of 5 channels so that the same sound can play simultaneously unless all of the channels are playing.

Note that the implementation of HTML5 Audio is buggy at best.

Example

Crafty.audio.play("walk");

//play and repeat forever
Crafty.audio.play("backgroundMusic", -1);
Crafty.audio.play("explosion",1,0.5); //play sound once with volume of 50%
Back to top

Crafty.audio.remove()

public this Crafty.audio.remove([String id])
id

A string to refer to sounds

Will stop the sound and remove all references to the audio object allowing the browser to free the memory. If no id is given, all sounds will be removed.

This function uses audio path set in Crafty.path in order to remove sound from the assets object.

Example

Crafty.audio.remove("walk");
Back to top

Crafty.audio.setChannels()

public this Crafty.audio.setChannels(Number n)
n

The maximum number of channels

Back to top

Crafty.audio.stop()

public this Crafty.audio.stop([Number ID])

Stops any playing sound. if id is not set, stop all sounds which are playing

Example

//all sounds stopped playing now
Crafty.audio.stop();
Back to top

Crafty.audio.supports()

public this Crafty.audio.supports(String extension)
extension

A file extension to check audio support for

Return true if the browser thinks it can play the given file type, otherwise false

Back to top

Crafty.audio.toggleMute()

public this Crafty.audio.toggleMute()

Mute or unmute every Audio instance that is playing. Toggles between pausing or playing depending on the state.

Example

//toggle mute and unmute depending on current state
Crafty.audio.toggleMute();
Back to top

Crafty.audio.togglePause()

public this Crafty.audio.togglePause(string ID)
{string} id

The id of the audio object to pause/

Toggle the pause status of the Audio instance specified by id param.

Example

Crafty.audio.togglePause('music');
Back to top

Crafty.audio.unmute()

public this Crafty.audio.unmute()

Unmute every Audio instance that is playing.

Example

Crafty.audio.unmute();
Back to top

Crafty.audio.unpause()

public this Crafty.audio.unpause(string ID)
{string} id

The id of the audio object to unpause

Resume playing the Audio instance specified by id param.

Example

Crafty.audio.unpause('music');