Skip to content

Instantly share code, notes, and snippets.

@kourge
Created September 13, 2010 17:16
Show Gist options
  • Save kourge/577659 to your computer and use it in GitHub Desktop.
Save kourge/577659 to your computer and use it in GitHub Desktop.
// https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Media_events
// https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIDOMHTMLMediaElement
var Sounds = {
sounds: {},
errors: [null, "ABORTED", "NETWORK", "DECODE", "SRC_NOT_SUPPORTED"],
register: function register(sounds) {
var errors = this.errors;
for (var i = 0, l = sounds.length; i < l; i++) {
var audio = new Audio();
audio.autobuffer = true;
audio.autoplay = false;
audio.src = sounds[i].src;
audio.addEventListener("error", function(event) {
throw new Error("MEDIA_ERR_" + errors[audio.error.code]);
}, false);
audio.load();
this.sounds[sounds[i].id] = audio;
}
},
play: function play(id) {
var audio = this.sounds[id];
if (!audio)
throw new Error("The id is not registered yet");
audio.play();
},
pauseAll: function pauseAll() {
for (var i = 0, l = this.sounds.length; i < l; i++) {
this.sounds[i].pause();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment