I wrote a basic script in Chrome that uses the new Web Audio Api to download 3 audio files (via XMLHTTPRequest) and play each one individually. I have provided a separate button for each sound, which allows the user to start and stop each sound.
The script immediately downloads all three sound files and, when this is done, smoothes the play buttons so that the user can only press play as soon as the sound is ready. In addition, the sounds are looped, so "Play" and "Stop" are changed on each button when the button is pressed.
All this works great ... When you press the Play button, you hear the sound of the loop and when you press Stop, the sound stops. However, when you try to play the same sound a second time, the sound will not start playing again. Each time you press the Play / Stop button, the corresponding playSound () or stopSound () functions are called and the corresponding parameters are passed, but for some reason I just canβt play the sounds a second time. Am I doing something wrong?
Here is my code:
<body> <label for="playBtn1">Moog:</label> <input id="playBtn1" type="button" value="Play" disabled /> <label for="playBtn1">Drums:</label> <input id="playBtn2" type="button" value="Play" disabled /> <label for="playBtn1">Choir:</label> <input id="playBtn3" type="button" value="Play" disabled /> <script> var playBtn1 = document.getElementById("playBtn1"); var playBtn2 = document.getElementById("playBtn2"); var playBtn3 = document.getElementById("playBtn3"); var context = new webkitAudioContext(); var soundBuffer1 = null; var soundBuffer2 = null; var soundBuffer3 = null; var soundBufferSourceNode1 = context.createBufferSource(); soundBufferSourceNode1.looping = true; var soundBufferSourceNode2 = context.createBufferSource(); soundBufferSourceNode2.looping = true; var soundBufferSourceNode3 = context.createBufferSource(); soundBufferSourceNode3.looping = true; loadSound('micromoog.wav', 1); loadSound('breakbeat-drum-loop.wav', 2); loadSound('choir.wav', 3); playBtn1.addEventListener("click", function(e) { if(this.value == "Play") { this.value = "Stop"; playSound(soundBuffer1, soundBufferSourceNode1); } else if(this.value == "Stop") { this.value = "Play"; stopSound(soundBufferSourceNode1); } }, false); playBtn2.addEventListener("click", function(e) { if(this.value == "Play") { this.value = "Stop"; playSound(soundBuffer2, soundBufferSourceNode2); } else if(this.value == "Stop") { this.value = "Play"; stopSound(soundBufferSourceNode2); } }, false); playBtn3.addEventListener("click", function(e) { if(this.value == "Play") { this.value = "Stop"; playSound(soundBuffer3, soundBufferSourceNode3); } else if(this.value == "Stop") { this.value = "Play"; stopSound(soundBufferSourceNode3); } }, false); function loadSound(url, bufferNum) { var request = new XMLHttpRequest(); request.open('GET', url, true); request.responseType = 'arraybuffer'; </script> </body>
Also, does anyone know of any event that can fire when sound is played without looping? It would be great if I could turn off these sounds, and as soon as the sound is played, use such an event to switch it. I donβt see anything in the specification, but maybe there is a better way?
Thanks, Brad.