Created
February 9, 2014 11:48
-
-
Save wojciak/8897983 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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