-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbnplay-audio-tag.html
171 lines (146 loc) Β· 4.39 KB
/
bnplay-audio-tag.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
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
<!doctype html>
<html>
<head>
<title>BNPlay - audio tag version</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="favicon.png" />
<style type="text/css">
body,
td,
th {
font-family: sans-serif;
}
body {
background-color: #222;
color: #ccc;
}
.text-center {
text-align: center;
}
.btn-ctrl {
background-color: #000;
color: #ccc;
border: 1px solid #888;
padding: 20px 30px;
font-weight: bold;
}
.btn-ctrl-loading {
color: #c60;
border: 1px solid #f80;
}
</style>
<script type="text/javascript">
// Brown Noise Player
class BNP {
static switchMillis = 30_000;
static player1;
static player2;
static interval;
static switchPlayer() {
if (this.player1.paused && !this.player2.paused) {
// Player 2 is playing. Switch to player 1
this.player1.play();
this.player2.pause();
this.player2.currentTime = 0;
} else if (!this.player1.paused && this.player2.paused) {
// Player 1 is playing. Switch to player 2
this.player2.play();
this.player1.pause();
this.player1.currentTime = 0;
} else if (this.player1.paused && this.player2.paused) {
// No player is playing. Start one player
this.player1.currentTime = 0;
this.player1.play();
} else if (!this.player1.paused && !this.player2.paused) {
// Both players are playing (this should never happen). Leave
// only player 1 playing
this.player2.pause();
this.player2.currentTime = 0;
}
}
static initialize(player1, player2) {
this.player1 = player1;
this.player2 = player2;
}
static isPlaying() {
return !(this.player1.paused && this.player2.paused);
}
static play() {
if (this.isPlaying()) return;
this.player2.currentTime = 0;
this.player1.play();
this.interval = setInterval(this.switchPlayer, this.switchMillis);
}
static stop() {
if (!this.isPlaying()) return;
clearInterval(this.interval);
this.player1.pause();
this.player2.pause();
this.player1.currentTime = 0;
this.player2.currentTime = 0;
}
static toggle() {
if (this.isPlaying()) this.stop();
else this.play();
}
}
class UI {
static load() {
BNP.initialize(
document.getElementById("player1"),
document.getElementById("player2"),
);
const btnToggle = document.getElementById("btnToggle");
btnToggle.classList.remove("btn-ctrl-loading");
btnToggle.removeAttribute("disabled");
this.update();
}
static update() {
const btnToggle = document.getElementById("btnToggle"),
divStatus = document.getElementById("divStatus");
if (BNP.isPlaying()) {
divStatus.textContent = "Playing...";
btnToggle.value = "STOP";
} else {
divStatus.textContent = "Stopped.";
btnToggle.value = "PLAY";
}
}
static toggle() {
BNP.toggle();
this.update();
}
}
</script>
</head>
<body
onload="UI.load()"
onkeypress="if (event.key == ' ') btnToggle.click();"
>
<audio hidden id="player1">
<source src="https://dmotte.github.io/bnplay/bn-sample.flac" />
Your browser does not support the HTML5 audio element
</audio>
<audio hidden id="player2">
<source src="https://dmotte.github.io/bnplay/bn-sample.flac" />
Your browser does not support the HTML5 audio element
</audio>
<div class="text-center">
<h1>Brown noise</h1>
<p><i>Audio tag version</i></p>
<br />
<div id="divStatus">Please wait...</div>
<br />
<input
type="button"
id="btnToggle"
value="Loading..."
class="btn-ctrl btn-ctrl-loading"
onfocus="this.blur()"
onclick="UI.toggle()"
disabled="disabled"
/>
</div>
</body>
</html>