-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusic.html
More file actions
238 lines (233 loc) · 7.92 KB
/
music.html
File metadata and controls
238 lines (233 loc) · 7.92 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!doctype html>
<html>
<head>
<title>Music player</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">(function () {
'use strict';
document.addEventListener('DOMContentLoaded', function () {
// helper function to assign multiple properties to an object recursively
function set(e, as) {
for (let a in as) {
if (as[a].constructor.name === 'Object') {
set(e[a], as[a]);
} else {
e[a] = as[a];
}
}
}
// helper function to create elements
function create(t, p, a) {
const e = document.createElement(t);
if (p) {
p.appendChild(e);
}
if (a) {
set(e, a);
}
return e;
}
// helper function to send an XMLHttpRequest
function xhr(file) {
const xhr = new XMLHttpRequest();
xhr.open('GET', file);
xhr.overrideMimeType('text/plain');
xhr.addEventListener('loadend', () => {
if (xhr.status === 200) {
listFiles(xhr.response);
}
});
xhr.send();
}
// helper function to track slider position
let originalTarget;
function dragSlider(mouseEvent, track, callback) {
// activate on left mouse button press
if (mouseEvent.buttons === 1) {
// save current event target
if (!originalTarget) {
originalTarget = mouseEvent.target;
}
// pass on slider percentage
if (originalTarget === track) {
callback(Math.min(Math.max((mouseEvent.clientX - track.getBoundingClientRect().left) / track.clientWidth * 100, 0), 100));
}
// deactivate on left mouse button release
} else {
originalTarget = undefined;
}
}
// helper function to create a slider specifically
function createSlider(p, w, h, init, color, callback) {
const track = create('div', p, {
style: {
display: 'inline-block',
width: w + 'px',
height: h + 'px',
backgroundImage: 'linear-gradient(to right, #000000, #7f7f7f)',
backgroundRepeat: 'no-repeat',
borderRadius: '200px'
}
});
const slider = create('div', track, {
style: {
width: init + '%',
height: '100%',
pointerEvents: 'none',
backgroundColor: color,
borderRadius: '200px'
}
});
// track slider position while dragging
window.addEventListener('mousemove', (e) => {
dragSlider(e, track, callback);
});
// track slider position on first click
track.addEventListener('mousedown', (e) => {
dragSlider(e, track, callback);
})
return slider;
}
// create a 'select' element, include all given elements as 'option's and save the according audios
function listFiles(list) {
let options, option;
select = create('select', document.body);
options = list.split('\n');
for (let o = 0; o < options.length; o++) {
if (options[o] !== '') {
option = create('option', select, {
value: o,
innerText: options[o]
});
audios.push(audio(options[o]));
}
}
}
// turn a relative path to an mp3 file into an audio element
function audio(file) {
const audio = create('audio', false, {
// loading of the actual sound file apparently starts asynchronous once this element is created which should be prevented
preload: 'none'
});
create('source', audio, {
src: file,
type: 'audio/mpeg'
});
return audio;
}
// update the timeline with the current time
function update() {
timelineSlider.style.width = (currentAudio.currentTime / currentAudio.duration * 100) + '%';
}
// TODO
function changeTrack(idx) {
}
let select, audios = [], currentAudio, currentAudioIdx, interval, volume = 0.1, timeline = 0;
// play button
const play = create('button', document.body, {
innerText: String.fromCharCode(0x23F5) // 'right arrow'
});
play.addEventListener('click', () => {
if (currentAudioIdx != select.value) {
if (currentAudio) {
currentAudio.pause();
}
currentAudio = undefined;
}
if (!currentAudio) {
currentAudio = audios[parseInt(select.value)].cloneNode(true);
currentAudio.volume = volume;
currentAudio.loop = Boolean(parseInt(loop.dataset.active));
currentAudioIdx = select.value;
currentAudio.addEventListener('durationchange', update);
currentAudio.addEventListener('timeupdate', update);
}
currentAudio.play();
});
// pause button
const pause = create('button', document.body, {
innerText: String.fromCharCode(0x23F8) // 'two blocks'
});
pause.addEventListener('click', () => {
if (currentAudio) {
currentAudio.pause();
}
if (interval) {
clearInterval(interval);
}
});
// previous button
const previous = create('button', document.body, {
innerText: String.fromCharCode(0x23EE) // 'double left arrow with block'
});
previous.addEventListener('click', () => {
if (currentAudio) {
currentAudio.pause();
}
});
// TODO: next button
// TODO: shuffle button
// loop button
const loop = create('button', document.body, {
innerText: String.fromCharCode(0x2B6E), // 'self-chasing arrow'
dataset: {
active: '1'
}
});
loop.addEventListener('click', () => {
set(loop, {
dataset: {
active: 1 - parseInt(loop.dataset.active)
},
style: {
color: Boolean(parseInt(loop.dataset.active)) ? 'grey' : 'black'
}
});
if (currentAudio) {
currentAudio.loop = Boolean(parseInt(loop.dataset.active));
}
});
// TODO: cross-fade button
/*
const cross = create('button', document.body, {
innerText: String.fromCharCode(0x292E) // 'intertwined arrows'
});
*/
// volume slider
const volumeSlider = createSlider(document.body, 100, 8, volume * 100, '#3f7fff', (percent) => {
volume = percent / 100;
if (currentAudio) {
currentAudio.volume = volume;
}
volumeSlider.style.width = percent + '%';
volumeDisplay.innerText = Math.floor(percent) + '%';
});
// volume display
const volumeDisplay = create('span', document.body, {
innerText: (volume * 100) + '%',
style: {
display: 'inline-block',
width: '40px',
textAlign: 'right',
padding: '0px 8px'
}
});
// timeline
const timelineSlider = createSlider(document.body, 400, 8, 0, '#7fff3f', (percent)=>{
if (currentAudio) {
currentAudio.currentTime = percent / 100 * currentAudio.duration;
update();
}
});
// request the file containing the relative paths to all the audio files
xhr('music.txt');
});
})();</script>
<style type="text/css">
* {
font-family: 'Segoe UI', sans-serif;
}
</style>
</head>
<body></body>
</html>