Advertisement
thetenfold

play-by-JoeSimmons-(html5)

Aug 6th, 2013
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement