2015-02-17 10:14:29 +00:00
|
|
|
function SoundBox() {
|
2015-02-17 10:12:05 +00:00
|
|
|
this.sounds = {};
|
|
|
|
this.sound_callbacks = {};
|
2015-02-19 20:17:16 +00:00
|
|
|
this.load = function(sound_name, path) {
|
2015-02-17 10:12:05 +00:00
|
|
|
this.sounds[sound_name] = new Audio(path);
|
|
|
|
// reset the sound ready for the next playing
|
2015-02-19 20:17:16 +00:00
|
|
|
this.sounds[sound_name].addEventListener("ended", (function(event) {
|
2015-02-17 10:12:05 +00:00
|
|
|
event.target.currentTime = 0;
|
|
|
|
if(typeof this.sound_callbacks[sound_name] == "function")
|
|
|
|
{
|
|
|
|
this.sound_callbacks[sound_name](sound_name);
|
2015-02-19 20:17:16 +00:00
|
|
|
delete this.sound_callbacks[sound_name];
|
2015-02-17 10:12:05 +00:00
|
|
|
}
|
2015-02-19 20:17:16 +00:00
|
|
|
}).bind(this));
|
2015-02-17 10:12:05 +00:00
|
|
|
};
|
|
|
|
|
2015-02-17 10:14:29 +00:00
|
|
|
this.remove = function(sound_name) {
|
2015-02-17 10:12:05 +00:00
|
|
|
if(typeof this.sounds != "undefined")
|
|
|
|
delete this.sounds[sound_name];
|
|
|
|
if(typeof this.sound_callbacks == "function")
|
|
|
|
delete this.sound_callbacks[sound_name];
|
|
|
|
};
|
|
|
|
|
2015-02-17 10:14:29 +00:00
|
|
|
this.play = function(sound_name, callback) {
|
2015-02-17 10:12:05 +00:00
|
|
|
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;
|
|
|
|
|
2015-02-19 20:17:16 +00:00
|
|
|
this.sounds[sound_name].play();
|
2015-02-17 10:12:05 +00:00
|
|
|
};
|
|
|
|
}
|