1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
window.addEventListener('load', () => { // noinspection JSUnresolvedVariable let audioCtx = new (window.AudioContext || window.webkitAudioContext)(); let xhr = new XMLHttpRequest(); xhr.open('GET', 'audio-autoplay.wav'); xhr.responseType = 'arraybuffer'; xhr.addEventListener('load', () => { let playsound = (audioBuffer) => { let source = audioCtx.createBufferSource(); source.buffer = audioBuffer; source.connect(audioCtx.destination); source.loop = false; source.start(); setTimeout(function () { let t = document.createElement('p'); t.appendChild(document.createTextNode((new Date()).toLocaleString() + ': Sound played')); document.querySelector('.output').appendChild(t); playsound(audioBuffer); }, 1000 + Math.random()*2500); }; audioCtx.decodeAudioData(xhr.response).then(playsound); }); xhr.send(); }); |