-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbutton.html
85 lines (72 loc) · 2.43 KB
/
button.html
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Button Click</title>
<link href="css/audio.css" rel="stylesheet" />
</head>
<body>
<div>
Button Click
</div>
<div>
<input type="button" id="clickButton" value="click" />
</div>
<div class="nav">
<a href="loop.html">> Looping</a><br/>
<a href="index.html">> Index</a>
</div>
</body>
<script type="text/javascript" src="js/audio-helper.js"></script>
<script type="text/javascript">
// two separate audio files
(function () {
var context = new AudioContext();
var audioHelper = new App.AudioHelper(context);
var downBuffer, upBuffer;
audioHelper.loadBuffer('audio/button/button-down.ogg', function (buffer) {
downBuffer = buffer;
});
audioHelper.loadBuffer('audio/button/button-up.ogg', function (buffer) {
upBuffer = buffer;
});
var button = document.querySelector('#clickButton');
button.addEventListener('mousedown', function () { playBuffer(downBuffer); });
button.addEventListener('mouseup', function () { playBuffer(upBuffer); });
function playBuffer(buffer) {
var source = context.createBufferSource();
source.buffer = buffer;
source.onended = function() {
source.disconnect();
};
source.connect(context.destination);
source.start();
}
})();
// single file containing audio sprites
// (function () {
// var context = new AudioContext();
// var audioHelper = new App.AudioHelper(context);
// var buttonBuffer;
//
// audioHelper.loadBuffer('audio/button/button-sprites.ogg', function (buffer) {
// buttonBuffer = buffer;
// });
//
// var button = document.querySelector('#clickButton');
// button.addEventListener('mousedown', function () { playBuffer(buttonBuffer, 0, 0.15); });
// button.addEventListener('mouseup', function () { playBuffer(buttonBuffer, 0.2, 0.15); });
//
// function playBuffer(buffer, offset, duration) {
// var source = context.createBufferSource();
// source.buffer = buffer;
// source.onended = function() {
// source.disconnect();
// };
//
// source.connect(context.destination);
// source.start(0, offset, duration);
// }
// })();
</script>
</html>