summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bieber_backwards.mp3bin0 -> 3412262 bytes
-rw-r--r--index.html3
-rw-r--r--test.mp3bin0 -> 3412262 bytes
-rw-r--r--webaudio.js74
4 files changed, 77 insertions, 0 deletions
diff --git a/bieber_backwards.mp3 b/bieber_backwards.mp3
new file mode 100644
index 0000000..5b20b6b
--- /dev/null
+++ b/bieber_backwards.mp3
Binary files 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 @@
+<html>
+<script type="text/javascript" src="webaudio.js"></script>
+</html>
diff --git a/test.mp3 b/test.mp3
new file mode 100644
index 0000000..5b20b6b
--- /dev/null
+++ b/test.mp3
Binary files 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');
+
+}