-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleSound.min.js
1 lines (1 loc) · 2.57 KB
/
simpleSound.min.js
1
var simpleSound=new function(){this.tagCheckInterval=100,this.extension="",this.contextAPI=void 0,this.APIbuffers={},this.APIinitQueue=[],this.APIplayQueue=[],this.tagID=0,this.tagList={},this.loadAPI=function(e){var n=new XMLHttpRequest;n.open("GET",e+this.extension,!0),n.responseType="arraybuffer",n.onload=function(){simpleSound.contextAPI.decodeAudioData(n.response,function(n){simpleSound.APIbuffers[e]=n,simpleSound.APIinitQueue.push(e);for(var t=0;t<simpleSound.APIplayQueue.length;t++)simpleSound.APIplayQueue[t]===e&&(simpleSound.play(e),simpleSound.APIplayQueue.splice(t--,1))})},n.send()},this.playAPI=function(e){if(void 0===simpleSound.APIbuffers[e])return void simpleSound.APIplayQueue.push(e);var n=simpleSound.contextAPI.createBufferSource();n.buffer=simpleSound.APIbuffers[e],n.connect(simpleSound.contextAPI.destination),"function"==typeof n.noteOn?n.noteOn(0):n.start()},this.initAPI=function(){if(0!==simpleSound.APIinitQueue.length)for(var e=0;e<simpleSound.APIinitQueue.length;e++){var n=simpleSound.APIbuffers[simpleSound.APIinitQueue[e]];if(void 0!==n){var t=simpleSound.contextAPI.createBufferSource(),i=simpleSound.contextAPI.createGain();i.gain.value=0,t.buffer=n,t.connect(i),i.connect(simpleSound.contextAPI.destination),"function"==typeof t.noteOn?t.noteOn(0):t.start(),simpleSound.APIinitQueue.splice(e--,1)}}},this.loadTag=function(e){var n="simpleSound_"+simpleSound.tagID++;simpleSound.tagList[e]=n;var t='<audio id="'+n+'" preload="auto" autobuffer>';t+=' <source src="'+e+'.ogg" type="audio/ogg" />',t+=' <source src="'+e+'.mp3" type="audio/mpeg" />',t+="</audio>",document.body.innerHTML+=t},this.checkTag=function(e){e.paused?document.body.removeChild(document.getElementById(e.id)):setTimeout(function(){simpleSound.checkTag(e)},simpleSound.tagCheckInterval)},this.playTag=function(e){if("undefined"!==simpleSound.tagList[e]){var n=document.getElementById(simpleSound.tagList[e]).cloneNode(!0);n.id="simpleSound_clone_"+simpleSound.tagID++,document.body.appendChild(n),setTimeout(function(){document.getElementById(n.id).play(),setTimeout(function(){simpleSound.checkTag(n)},simpleSound.tagCheckInterval)},10)}},"undefined"!=typeof AudioContext||"undefined"!=typeof window.webkitAudioContext?(this.load=this.loadAPI,this.play=this.playAPI,this.contextAPI="undefined"!=typeof AudioContext?new window.AudioContext:new window.webkitAudioContext,document.addEventListener("touchstart",function(){simpleSound.initAPI()})):(this.load=this.loadTag,this.play=this.playTag);var e=document.createElement("audio");this.extension=e.canPlayType&&e.canPlayType("audio/mpeg;").replace(/no/,"")?".mp3":".ogg"};