greenmelon

click sound effect

Nov 30th, 2021 (edited)
6,615
4
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 4 0
  1. <script>
  2.  
  3. // Mouseover/ Click sound effect- by JavaScript Kit (www.javascriptkit.com)
  4. // Visit JavaScript Kit at http://www.javascriptkit.com/ for full source code
  5.  
  6. //** Usage: Instantiate script by calling: var uniquevar=createsoundbite("soundfile1", "fallbackfile2", "fallebacksound3", etc)
  7. //** Call: uniquevar.playclip() to play sound
  8.  
  9. var html5_audiotypes={ //define list of audio file extensions and their associated audio types. Add to it if your specified audio file isn't on this list:
  10. "mp3": "audio/mpeg",
  11. "mp4": "audio/mp4",
  12. "ogg": "audio/ogg",
  13. "wav": "audio/wav"
  14. }
  15.  
  16. function createsoundbite(sound){
  17. var html5audio=document.createElement('audio')
  18. if (html5audio.canPlayType){ //check support for HTML5 audio
  19. for (var i=0; i<arguments.length; i++){
  20. var sourceel=document.createElement('source')
  21. sourceel.setAttribute('src', arguments[i])
  22. if (arguments[i].match(/\.(\w+)$/i))
  23. sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
  24. html5audio.appendChild(sourceel)
  25. }
  26. html5audio.load()
  27. html5audio.playclip=function(){
  28. html5audio.pause()
  29. html5audio.currentTime=0
  30. html5audio.play()
  31. }
  32. return html5audio
  33. }
  34. else{
  35. return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio unfortunately")}}
  36. }
  37. }
  38.  
  39. //Initialize two sound clips with 1 fallback file each:
  40.  
  41. var mouseoversound=createsoundbite("https://a.tumblr.com/tumblr_ojrn7aGBii1w2e2oyo1.mp3")
  42. var clicksound=createsoundbite("https://a.tumblr.com/tumblr_ojrmy55yUN1w2e2oyo1.mp3")
  43.  
  44. </script>
Add Comment
Please, Sign In to add comment