-
Notifications
You must be signed in to change notification settings - Fork 4
/
demo.html
108 lines (100 loc) · 3.52 KB
/
demo.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"/>
<meta name="format-detection" content="telephone=no"/>
<title>Benz Audio Engine Demo</title>
</head>
<body>
<h1>Benz Audio Engine 演示</h1>
<p>
<label for="isSupport">是否支持:</label>
<strong id="isSupport"></strong>
</p>
<p>
<label for="volume">音量:</label>
<input id="volume" type="range" value="1" step="0.01" max="1" min="0" disabled>
</p>
<p>
<label>循环音乐:</label>
<input type="button" id="music_play" disabled value="播放">
<input type="button" id="music_pause" disabled value="暂停">
<input type="button" id="music_stop" disabled value="停止">
</p>
<p>
<label>音效:</label>
<input type="button" id="jump" disabled value="跳!"/>
<input type="button" id="gold" disabled value="金币!"/>
<input type="button" id="kick" disabled value="踩!"/>
<input type="button" id="sprite" disabled value="一小段sprite"/>
</p>
<script src="benzAudioEngine.js"></script>
<script>
(function () {
var getEl = function (id) {
return document.getElementById(id);
};
// 检测是否支持 Web Audio API
getEl('isSupport').innerHTML = benzAudioEngine.support() ? 'Yes!' : 'No.';
if (benzAudioEngine.support()) {
getEl('volume').removeAttribute('disabled');
}
// 设置音量
getEl('volume').onchange = function () {
benzAudioEngine.setVolume(this.value);
};
// 加载音频资源
benzAudioEngine.load('res/mario.mp3', function () {
getEl('music_play').removeAttribute('disabled');
getEl('music_pause').removeAttribute('disabled');
getEl('music_stop').removeAttribute('disabled');
getEl('sprite').removeAttribute('disabled');
});
benzAudioEngine.load('res/jump.mp3', function () {
getEl('jump').removeAttribute('disabled');
});
benzAudioEngine.load('res/gold.mp3', function () {
getEl('gold').removeAttribute('disabled');
});
benzAudioEngine.load('res/kick.mp3', function () {
getEl('kick').removeAttribute('disabled');
});
benzAudioEngine.sprite('res/mario.mp3', {
'a sprite' : [2.0, 6.0]
});
// 播放/继续播放循环音乐,循环从2.40秒到88.80秒
var marioID = 0;
getEl('music_play').onclick = function () {
if (marioID) {
benzAudioEngine.resume(marioID);
} else {
marioID = benzAudioEngine.play('res/mario.mp3', 2.40, 88.80);
}
};
// 暂停循环音乐
getEl('music_pause').onclick = function () {
benzAudioEngine.pause(marioID);
};
// 停止循环音乐
getEl('music_stop').onclick = function () {
benzAudioEngine.stop(marioID);
marioID = 0;
};
// 播放各种音效
getEl('jump').onclick = function () {
benzAudioEngine.play('res/jump.mp3');
};
getEl('gold').onclick = function () {
benzAudioEngine.play('res/gold.mp3');
};
getEl('kick').onclick = function () {
benzAudioEngine.play('res/kick.mp3');
};
getEl('sprite').onclick = function () {
benzAudioEngine.play('a sprite');
};
})();
</script>
</body>
</html>