forked from sheix/html5udacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstractXMLHttpRequest.js
57 lines (48 loc) · 1.64 KB
/
abstractXMLHttpRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Fill out the provided xhrGet function to abstract
// out the functionality of performing an XMLHttpRequest
// GET request.
//
// The provided parameters are the URI to make the request
// to, the callback to call at onload, and the responseType,
// if necessary. If we don't need a special responseType,
// assume that that parameter is null.
//
// Now, we're going to assume that the callback takes the
// request object as a parameter, instead of taking no
// parameters.
//
// We've provided you modified versions of the previous
// two callbacks below. At the bottom of the file, we call
// xhrGet with both callbacks to help you with testing your
// code.
function xhrGet(reqUri, callback, type) {
var xhrt = new XMLHttpRequest();
xhrt.responseType = type;
xhrt.open("GET",reqUri,true);
xhrt.onload = function() {callback(this);};
return xhrt;
}
parseJSON = function (xhr) {
var parsedJSON = JSON.parse(xhr.responseText);
var x = parsedJSON['frames']['chaingun_impact.png']['spriteSourceSize']['x'];
console.log(x);
return x;
};
playSound = function (xhr) {
try {
var context = new webkitAudioContext();
var mainNode = context.createGainNode(0);
mainNode.connect(context.destination);
var clip = context.createBufferSource();
context.decodeAudioData(xhr.response, function (buffer) {
clip.buffer = buffer;
clip.gain.value = 1.0;
clip.connect(mainNode);
clip.loop = true;
clip.noteOn(0);
}, function (data) {});
}
catch(e) {
console.warn('Web Audio API is not supported in this browser');
}
};