1
0
Fork 0

Create soundbox.js

Untested
This commit is contained in:
Starbeamrainbowlabs 2015-02-17 10:12:05 +00:00
parent 1382633607
commit 29897186e7
1 changed files with 35 additions and 0 deletions

35
soundbox.js Normal file
View File

@ -0,0 +1,35 @@
soundbox = function() {
this.sounds = {};
this.sound_callbacks = {};
this.add_sound = function(sound_name, path) {
this.sounds[sound_name] = new Audio(path);
// reset the sound ready for the next playing
this.sounds[sound_name].addEventListener("ended", function(event) {
event.target.currentTime = 0;
if(typeof this.sound_callbacks[sound_name] == "function")
{
this.sound_callbacks[sound_name](sound_name);
}
});
};
this.remove_sound = function(sound_name) {
if(typeof this.sounds != "undefined")
delete this.sounds[sound_name];
if(typeof this.sound_callbacks == "function")
delete this.sound_callbacks[sound_name];
};
this.play_sound = function(sound_name, callback) {
if(typeof this.sounds[sound_name] == "undefined")
{
console.error("Can't find sound called '" + sound_name + "'.");
return false;
}
if(typeof callback == "function")
this.sound_callbacks[sound_name] = callback;
this..sounds[sound_name].play();
};
}