Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

play-by-JoeSimmons-(html5)

By: thetenfold on Aug 6th, 2013  |  syntax: JavaScript  |  size: 0.75 KB  |  views: 66  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // play by JoeSimmons
  2. function play(src) {
  3.     var elem = document.getElementById('sound_player'),
  4.         body = document.body;
  5.  
  6.     src = src.replace(/\s/g, '%20').replace(/\\/g, '/');
  7.  
  8.     if (!elem) {
  9.         elem = document.createElement('audio');
  10.         elem.src = src;
  11.         elem.id = 'sound_player';
  12.         elem.setAttribute('autoplay', '');
  13.         elem.setAttribute('preload', 'auto');
  14.         elem.setAttribute('style', 'width: 1px; height: 1px; display: inline; visibility: hidden; position: fixed; top: 0; left: -9999px;');
  15.         if (body) {
  16.             body.appendChild(elem);
  17.         }
  18.     } else {
  19.         if (elem.src !== src) {
  20.             elem.src = src;
  21.         } else {
  22.             elem.play();
  23.         }
  24.     }
  25. }