Last active
August 29, 2015 14:09
-
-
Save dannyvassallo/2077813ca92768482798 to your computer and use it in GitHub Desktop.
Play Audio on Click
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
<div class="play">Play</div> |
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
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