-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
now Play the Sound File whenever the X key is pressed
- Loading branch information
Mehrad Kavian
authored and
Mehrad Kavian
committed
May 22, 2015
1 parent
860e87a
commit 9303033
Showing
3 changed files
with
49 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<!doctype html> | ||
<html lang="en-US" direction="ltr"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Web Audio Test 4 | Buffer And Load a Sound File</title> | ||
<script src="jquery.js" ></script> | ||
|
||
</head> | ||
<body> | ||
<div> | ||
|
||
</div> | ||
<script> | ||
(function () { | ||
var context = new AudioContext(); | ||
var electro; | ||
|
||
var getAudio = new XMLHttpRequest(); | ||
getAudio.open("GET","snare.wav",true); | ||
getAudio.responseType = "arraybuffer"; | ||
|
||
getAudio.onload = function () { | ||
context.decodeAudioData(getAudio.response,function(buffer){ //decode file and then Store it in variable electro | ||
electro = buffer; | ||
}); | ||
} | ||
getAudio.send(); | ||
|
||
window.addEventListener("keydown",onKeyDown); | ||
|
||
function onKeyDown (e){ | ||
switch (e.keyCode) { | ||
case 88 : | ||
var playSound = context.createBufferSource(); | ||
playSound.buffer = electro; | ||
|
||
playSound.connect(context.destination); | ||
playSound.start(0); | ||
break; | ||
} | ||
} | ||
|
||
})(); | ||
|
||
|
||
|
||
</script> | ||
</body> | ||
</html> |