From f5e6d9c6cfd8a4ecf695e670a00ddd57eb21364c Mon Sep 17 00:00:00 2001 From: yo mama Date: Wed, 10 Jun 2015 19:40:45 -0700 Subject: added necessary js --- bieber_backwards.mp3 | Bin 0 -> 3412262 bytes index.html | 3 +++ test.mp3 | Bin 0 -> 3412262 bytes webaudio.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 bieber_backwards.mp3 create mode 100644 index.html create mode 100644 test.mp3 create mode 100644 webaudio.js diff --git a/bieber_backwards.mp3 b/bieber_backwards.mp3 new file mode 100644 index 0000000..5b20b6b Binary files /dev/null and b/bieber_backwards.mp3 differ diff --git a/index.html b/index.html new file mode 100644 index 0000000..ee98701 --- /dev/null +++ b/index.html @@ -0,0 +1,3 @@ + + + diff --git a/test.mp3 b/test.mp3 new file mode 100644 index 0000000..5b20b6b Binary files /dev/null and b/test.mp3 differ 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'); + +} -- cgit v1.2.3-70-g09d2