minijsound a minimal javascript sound engine

minijsound is a minimal javascript sound engine class with preload and without the need of audio tags.

In less than 1Kb you have a simple class to declare your sounds URLs, to preload all sounds (useful for html5 games or multimedia web apps) and to play them immediately having back a reference of the Audio object (to pause it, replay it, etc..).

No audio TAG is required in your DOM.

Enjoy it!
d

Source code available on JSFiddle
http://jsfiddle.net/onyrix/8ackmqea/

minijsound – minimal javascript sound engine – source code

// minijsound
// a minimal javascript sound library with preload
// written by Dino Olivieri
// http://www.onyrix.com
//
var minijsound = {
soundLibrary:{
sound1:”https://upload.wikimedia.org/wikipedia/commons/0/01/01_Pista_1.ogg”,
sound2:”https://upload.wikimedia.org/wikipedia/commons/4/43/Ubuntu_sound.ogg”
},
soundCache:{},
preLoadSounds:function(){
for(var k in this.soundLibrary){
this.soundCache[k] = new Audio();
this.soundCache[k].src = this.soundLibrary[k];
this.soundCache[k].load();
console.log(‘preloaded sound: ‘+this.soundCache[k].src);
}
},
playSound:function(k){
try{
return this.soundCache[k].play();
}catch(err){
console.log(‘playSound:error ‘+err.message);
}
}
}
// example
minijsound.preLoadSounds();
var s1 = minijsound.playSound(“sound1”);
var s2 = minijsound.playSound(“sound2”);

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close