Please follow and like us:
Facebook
Google+
http://www.onyrix.com/2015/09/24/minijsound-minimal-javascript-sound-engine/
Twitter
Pinterest
LinkedIn
YouTube
Instagram

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”);