diff options
| author | yo mama <pepper@scannerjammer.com> | 2015-06-10 19:40:45 -0700 |
|---|---|---|
| committer | yo mama <pepper@scannerjammer.com> | 2015-06-10 19:40:45 -0700 |
| commit | f5e6d9c6cfd8a4ecf695e670a00ddd57eb21364c (patch) | |
| tree | 90f5f4272199c901de293f9772f8ae3a97484d0c | |
| parent | 2bf66a74f82562eb3f46ae40d051ff4db2af52a0 (diff) | |
| -rw-r--r-- | bieber_backwards.mp3 | bin | 0 -> 3412262 bytes | |||
| -rw-r--r-- | index.html | 3 | ||||
| -rw-r--r-- | test.mp3 | bin | 0 -> 3412262 bytes | |||
| -rw-r--r-- | webaudio.js | 74 |
4 files changed, 77 insertions, 0 deletions
diff --git a/bieber_backwards.mp3 b/bieber_backwards.mp3 Binary files differnew file mode 100644 index 0000000..5b20b6b --- /dev/null +++ b/bieber_backwards.mp3 diff --git a/index.html b/index.html new file mode 100644 index 0000000..ee98701 --- /dev/null +++ b/index.html @@ -0,0 +1,3 @@ +<html> +<script type="text/javascript" src="webaudio.js"></script> +</html> diff --git a/test.mp3 b/test.mp3 Binary files differnew file mode 100644 index 0000000..5b20b6b --- /dev/null +++ b/test.mp3 diff --git a/webaudio.js b/webaudio.js new file mode 100644 index 0000000..71c1dc4 --- /dev/null +++ b/webaudio.js @@ -0,0 +1,74 @@ +//creating an audio context +var context; +var audioBuffer; +window.addEventListener('load', init); + +function init() +{ + try + { + window.AudioContext = window.AudioContext || window.webkitAudioContext; + context=new AudioContext(); + } + catch(e) + { + alert("Your browser doesn't support Web Audio API"); + } + initAudio(); +} + +function initAudio(){ + console.log("audio ready"); + console.log("inside audio initialized callback"); + var url1 = "someurl" + var url2 = "someotherurl" + loadAndPlay(url1, function(){ + loadAndPlay(url2, function(){ + console.log("played both urls"); + }); + }); + + +} +function loadAndPlay(url, callback) { + var audioURL=url; + + //creating a new request + var request = new XMLHttpRequest(); + request.open("GET",audioURL,true); + request.responseType= 'arraybuffer'; + request.onload = function(){ + //take the audio from http request and decode it in an audio buffer + context.decodeAudioData(request.response, function(buffer){ + audioBuffer = buffer; + console.log(audioBuffer); + if(audioBuffer){ // check here + playSound(callback); + } + }); + + }; + + request.send(); +} + +//playing the audio file +function playSound(callback) { + + //creating source node + var source = context.createBufferSource(); + //passing in file + source.buffer = audioBuffer; + source.onended = function(){ + console.log("inside ended callback"); + console.log("ended"); + if (typeof(callback) != "undefined" ){ + callback(); + } + } + //start playing + source.connect(context.destination); // added + source.start(0); + console.log('playing'); + +} |
