mirror of
https://github.com/sbrl/soundbox.git
synced 2018-01-10 21:33:43 +00:00
Create soundbox.js
Untested
This commit is contained in:
parent
1382633607
commit
29897186e7
1 changed files with 35 additions and 0 deletions
35
soundbox.js
Normal file
35
soundbox.js
Normal 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();
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in a new issue