-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
181 lines (181 loc) · 6.01 KB
/
audio.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//audio
var gameAudio = {
files : [
'GW2_ui',
'GW2_ingame',
'bullet_hitwall',
//'Enemy_explode', // don't like this sound
//'Enemy_red_hit', // don't like this sound
'Enemy_red_suck',
'Enemy_red_warning',
'Enemy_spawn_blue',
'Enemy_spawn_green',
'Enemy_spawn_orange',
'Enemy_spawn_red',
'Fire_Hispeed', // this would be better if I could make it faster
'Fire_homing', // this is an awful sound
'Fire_normal', // this would be better if I could make it faster
'Fire_smartbomb',
'Fire_smartbomb_low',
'Fire_triway',
'Game_over',
'Game_start',
'Gravity_well_die',
'Gravity_well_explode',
'Hi_Score_achieved',
'Mayfly', //this could be shorter
'Multiplier',
'pickup_extralife',
'pickup_smartbomb',
'pickup_weapon',
'Player_shielded_hit_enemy',
'Player_Spawn',
'SFX_ambience',
'Shield_off',
'Shield_on',
'Ship_explode',
'Ship_hitwall',
'Ship_thrust_loop',
'silence',
'Snake_spawn',
'UI_Change',
'UI_Confirm',
'UI_Delete',
'Wanderer_spawn'
],
ready:false,
effects:true,
music:true,
_:{},
support:null,
load:function(){
var browser = navigator.userAgent;
if (browser.indexOf('MSIE')>-1)
browser = 'ie';
else if (browser.indexOf('Firefox')>-1)
browser = 'ff';
else if (browser.indexOf('WebKit')>-1)
browser = 'wk';
if (!gameAudio.support){
gameAudio.loaded();
return;
}
var i = 0, l = gameAudio.files.length;
function loopMe(){
this.play();
}
function resetMe(){
this.currentTime = 0;
this.pause();
}
function loading(e){
if (this.readyState == 4 && !this.isLoaded){
this.isLoaded = true;
//document.getElementById('debug').innerHTML += '<div class="success">Success : '+this.id+'</div>';
loadNextAudio();
}
}
function loadNextAudio(){
if (i>=l){
gameAudio.loaded();
return;
}
var f = gameAudio.files[i++],
a = gameAudio._[f] = document.createElement('audio');
//console.warn(f);
a.id = f;
switch(browser){
case 'wk':
a.addEventListener('durationchange',loading,true); // chrome does not support the progress event right now
break;
case 'ff':
default:
a.addEventListener('progress',loading,true); // this is the only thing I should have to use
break;
}
if (f === 'GW2_ui' || f === 'GW2_ingame'){
a.addEventListener('ended',loopMe,true);
}else{
a.addEventListener('ended',resetMe,true);
}
a.src = "audio/"+gameAudio.support+"/"+f+"."+gameAudio.support;
//document.getElementById('debug').innerHTML += '<div>Loading : '+f+'</div>';
try {
a.load();
gameAudio._[f] = a;
}catch(er){
delete gameAudio._[f];
//document.getElementById('debug').innerHTML += '<div class="failed">Failed : '+f+'<br>'+er.message+'</div>';
loadNextAudio();
}
if (browser == 'ie')
setTimeout(loadNextAudio,100); // IE doesn't support any of the media events properly
}
setTimeout(loadNextAudio,10);
},
loaded:function(){
if (gameAudio.ready)
return;
gameAudio.ready = true;
if (gameAudio.music){
gameAudio.play(document.body.childNodes[0].className == 'game'?'GW2_ingame':'GW2_ui');
}
},
play:function(id){
if (!gameAudio.ready || !gameAudio._[id])
return;
if (id.indexOf("GW2") === 0){
if (!gameAudio.music)
return;
}else {
if (!gameAudio.effects)
return;
}
gameAudio._[id].currentTime = 0;
gameAudio._[id].play();
},
stop:function(t){
if (!gameAudio.ready)
return;
var _ = gameAudio._;
for (var i in _){
var a = _[i];
if (!t || (t === 'music' && i.indexOf('GW2') === 0) || (t === 'effects' && i.indexOf('GW2') !== 0)){
a.pause();
a.currentTime = 0;
}
}
},
toggle:function(t){
if (!gameAudio.ready)
return;
if (t == 'music'){
var e = gameAudio.music = !gameAudio.music;
if (e){
gameAudio.play(document.body.childNodes[0].className == 'game'?'GW2_ingame':'GW2_ui');
}else {
gameAudio.stop('music');
}
}else {
var e = gameAudio.effects = !gameAudio.effects;
if (e)
gameAudio.play('Ship_explode');
else
gameAudio.stop('effects');
}
},
init:function(){
var myAudio = document.createElement('audio');
if (!myAudio.canPlayType)
return;
var ogg = myAudio.canPlayType('audio/ogg'),
mp3 = myAudio.canPlayType('audio/mp3'),
wav = myAudio.canPlayType('audio/wave');
ogg = (ogg && ogg!='no')?true:false;
mp3 = (mp3 && mp3!='no')?true:false;
wav = (wav && wav!='no')?true:false;
gameAudio.support = ogg?'ogg':mp3?'mp3':wav?'wav':false;
document.getElementById('audioused').innerHTML = gameAudio.support;//debugging data
gameAudio.load();
}
}