Created
July 22, 2013 16:07
-
-
Save nickdiego/6055096 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head></head> | |
| <body> | |
| <script> | |
| var audioContext; | |
| var audioURL = "http://localhost:8000/basic-tests/chicken.ogg"; | |
| function loadSound(url, callback) { | |
| var request = new XMLHttpRequest(); | |
| request.open('GET', url, true); | |
| request.responseType = 'arraybuffer'; | |
| // Decode asynchronously | |
| request.onload = function() { | |
| audioContext.decodeAudioData(request.response, callback, | |
| function(err) { | |
| console.error("Error decoding buffer :( -> " + err); | |
| } | |
| ); | |
| }; | |
| request.onerror = function(err) { | |
| console.error("Error downloading song :("); | |
| }; | |
| console.log("Downloading audio..."); | |
| request.send(); | |
| } | |
| function playSound(buffer) { | |
| var source = audioContext.createBufferSource(); // creates a sound source | |
| source.buffer = buffer; // tell the source which sound to play | |
| source.connect(audioContext.destination); // connect the source to the context's destination (the speakers) | |
| source.start(0); // play the source now | |
| } | |
| try { | |
| window.AudioContext = window.AudioContext||window.webkitAudioContext; | |
| audioContext = new AudioContext(); | |
| console.log("AudioContext ok!"); | |
| loadSound(audioURL, function(buffer) { | |
| console.log("Fully loaded, ready to play :)"); | |
| playSound(buffer); | |
| }); | |
| } | |
| catch(e) { | |
| console.error('Error :( ' + e); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment