-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.oz.js
95 lines (84 loc) · 2.25 KB
/
audio.oz.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
OZ.Audio = {
format: "",
supported: false,
template: "{name}.{format}",
play: function(name) {
if (!OZ.Audio.supported) { return null; }
var a = new Audio(this._format(name));
a.play();
return a;
},
background: {
template: "{name}.{format}",
queue: [],
format: "",
_audio: null,
play: function() {
if (!OZ.Audio.supported) { return; }
if (!this._audio.src) { this.next(); }
if (this._audio.src && this._audio.paused) { this._audio.play(); }
},
pause: function() {
if (!OZ.Audio.supported) { return; }
if (!this._audio.paused) { this._audio.pause(); }
},
next: function() {
if (!OZ.Audio.supported) { return; }
if (!this.queue.length) { return; }
var paused = this._audio.paused;
var name = this.queue.shift();
this.queue.push(name);
this._audio.src = this._format(name);
if (!paused) { this.play(); }
},
previous: function() {
if (!OZ.Audio.supported) { return; }
if (!this.queue.length) { return; }
var paused = this._audio.paused;
var name = this.queue.pop();
this.queue.unshift(name);
name = this.queue[this.queue.length-1];
this._audio.src = this._format(name);
if (!paused) { this.play(); }
},
fadeOut: function(time) {
if (!OZ.Audio.supported) { return; }
var volume = 1;
var diff = 1 / ((time || 3) * 10);
var interval = setInterval(function() {
volume = Math.max(0, volume-diff);
this._audio.volume = Math.pow(volume, 2);
if (!this._audio.volume) {
clearInterval(interval);
this.next();
this._audio.volume = 1;
}
}.bind(this), 100);
}
},
_init: function() {
this.supported = !!window.Audio;
if (!this.supported) { return; }
this.format = (new Audio().canPlayType("audio/ogg") ? "ogg" : "mp3");
this.background.format = this.format;
this.background._format = this._format;
this.background._audio = new Audio();
OZ.Event.add(this.background._audio, "ended", function() {
this.next();
this.play();
}.bind(this.background));
this.background.next();
},
_format: function(name) {
var subst = {
"format": this.format,
"name": name
}
var str = this.template;
for (var p in subst) {
str = str.replace(new RegExp("{"+p+"}", "g"), subst[p]);
}
return str;
}
};
OZ.Audio._init();