Skip to content

Instantly share code, notes, and snippets.

@dannyvassallo
Last active August 29, 2015 14:09
Show Gist options
  • Save dannyvassallo/2077813ca92768482798 to your computer and use it in GitHub Desktop.
Save dannyvassallo/2077813ca92768482798 to your computer and use it in GitHub Desktop.
Play Audio on Click
<div class="play">Play</div>
var audiotypes={
"mp3": "audio/mpeg",
"mp4": "audio/mp4",
"ogg": "audio/ogg",
"wav": "audio/wav"
}
function ss_soundbits(sound){
var audio_element = document.createElement('audio')
if (audio_element.canPlayType){
for (var i=0; i<arguments.length; i++){
var source_element = document.createElement('source')
source_element.setAttribute('src', arguments[i])
if (arguments[i].match(/\.(\w+)$/i))
source_element.setAttribute('type', audiotypes[RegExp.$1])
audio_element.appendChild(source_element)
}
audio_element.load()
audio_element.playclip=function(){
audio_element.pause()
audio_element.currentTime=0
audio_element.play()
}
return audio_element
}
}
var clicksound = ss_soundbits('your/path/to/click.ogg', "https://s3.amazonaws.com/myfangate.com/soundtest/Dog+Woof-SoundBible.com-457935112.mp3");
var plopsound = ss_soundbits('your/path/to/plopp.ogg', "your/path/to/plopp.mp3");
$('.play').click(function(){
clicksound.playclip();
});
$('.play').mouseenter(function(){
plopsound.playclip();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment