-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioManager.js
43 lines (35 loc) · 1013 Bytes
/
audioManager.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
function AudioManager(audioPath) {
audioPath = audioPath || "";
var audios = {},
audioExt = getSupportedFileTypeExt();
function getSupportedFileTypeExt() {
var audio = $("<audio>")[0];
if (audio.canPlayType("audio/ogg")) return ".ogg";
if (audio.canPlayType("audio/mpeg")) return ".mp3";
if (audio.canPlayType("audio.wav")) return ".wav";
return "";
};
this.getAudio = function(name, onLoaded, onError) {
var audio = audios[name];
if(!audio) {
audio = createAudio(name, onLoaded, onError);
//Add to cache
audios[name] = audio;
} else if (onLoaded) {
onLoaded(audio);
}
return audio;
};
function createAudio(name, onLoaded, onError) {
var audio = $("<audio>")[0];
audio.addEventListener("canplaythrough", function() {
if (onLoaded) onLoaded(audio);
audio.removeEventListener("canplaythrough", arguments.callee);
});
audio.onerror = function() {
if (onError) onError(audio);
};
audio.src = audioPath + "/" + name + audioExt;
return audio;
}
}