Skip to content

Instantly share code, notes, and snippets.

@wojciak
Created February 9, 2014 11:48
Show Gist options
  • Save wojciak/8897983 to your computer and use it in GitHub Desktop.
Save wojciak/8897983 to your computer and use it in GitHub Desktop.
function loadSound(name) {
function () {
'use strict';
var init,
Howl = Howl || null,
//change this
assetSource = 'path/to/sounds';
init = function (name) {
var sfx;
if (Howl) {
sfx = new Howl({
urls: [assetSource + name + '.mp4', assetSource + name + '.mp3']
});
} else {
sfx = document.createElement('audio');
if (sfx.canPlayType('audio/mp4')) {
sfx.src = assetSource + name + '.mp4';
}
if (sfx.canPlayType('audio/mp3')) {
sfx.src = assetSource + name + '.mp3';
}
sfx.preload = true;
sfx.loop = false;
sfx.load();
document.body.appendChild(sfx);
}
//returns a function that plays the sounds
return function () {
sfx.play();
};
};
this.on(init);
}
}
//load
var boom = loadSound('boom');
//play
boom();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment