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.
public this Crafty.audio.add(String id, String url)
public this Crafty.audio.add(String id, Array urls)
public this Crafty.audio.add(Object map)
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
.
//adding audio from an object
Crafty.audio.add({
shoot: ["sounds/shoot.wav",
"sounds/shoot.mp3",
"sounds/shoot.ogg"],
coin: "sounds/coin.mp3"
});
//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");
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)
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.
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%
public this Crafty.audio.remove([String id])
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.
Crafty.audio.remove("walk");