-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
182 lines (147 loc) · 5.1 KB
/
index.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
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>kubs</title>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript">
var songInfo = [
{
id: "breathe",
fileLocation: "fl/masters/140820.flac",
fileType: "audio/flac",
coverLocation: "covers/breathe.jpg",
title: "Breathe",
album: "",
artist:"kubs"
},
/*
{
id: "miles-away",
fileLocation: "fl/masters/280320.wav",
fileType: "audio/wav",
coverLocation: "covers/miles-away.jpg",
title: "Miles Away",
album: "f**k i think i made another rap track",
artist:"kubs"
},
{
id: "suicide-by-train",
fileLocation: "fl/masters/290320.flac",
fileType: "audio/flac",
coverLocation: "covers/suicide-by-train.jpg",
title: "Suicide By Train",
album: "f**k i think i made another rap track",
artist:"kubs"
}*/
]
var setContent = function(elem, query, text) {
elem.querySelector(query).innerHTML = text
}
var setSongInfo = function(elem, songInfo) {
setContent(elem, ".title", songInfo.title)
setContent(elem, ".album", songInfo.album)
setContent(elem, ".artist", songInfo.artist)
var source = elem.querySelector("source")
source.src = songInfo.fileLocation
source.type = songInfo.fileType
var cover = elem.querySelector("img.cover")
cover.src = songInfo.coverLocation
var audio = elem.querySelector("audio")
var record = elem
var playstate = elem.querySelector(".playstate")
var timePlayed = elem.querySelector('.timeplayed')
var timeRemaining = elem.querySelector('.timeremaining')
/* Updates the time displays in the overlay according to audio.currentTime */
var updateTime = function(audio, timePlayed, timeRemaining) {
return function() {
var floor = Math.floor
var totalPlayedSecs = floor(audio.currentTime)
var totalRemainingSecs = floor(audio.duration - audio.currentTime)
var playedMins = floor(totalPlayedSecs / 60)
var playedSecs = floor(totalPlayedSecs % 60)
var remainingMins = floor(totalRemainingSecs / 60)
var remainingSecs = floor(totalRemainingSecs % 60)
if(audio.currentTime) {
timePlayed.innerHTML = playedMins.toString().padStart(2, "0") + ":" + playedSecs.toString().padStart(2, "0")
}
if(audio.duration) {
timeRemaining.innerHTML = "-" + remainingMins.toString().padStart(2, "0") + ":" + remainingSecs.toString().padStart(2, "0")
}
}
}
// Initialize all the displays
var updateTimeFn = updateTime(audio, timePlayed, timeRemaining)
updateTimeFn()
audio.addEventListener('play', function() {
record.classList.add('playing')
playstate.innerHTML = "⏸"
})
audio.addEventListener('pause', function() {
record.classList.remove('playing')
playstate.innerHTML = "▶️"
})
audio.addEventListener('timeupdate', updateTimeFn)
record.addEventListener('click', function() {
// Pause all other tracks
document.querySelectorAll('.record').forEach(function(elem) {
if(elem != record) {
elem.querySelector('audio').pause()
elem.classList.remove('active')
}
})
record.classList.add('active')
if(audio.paused) {
audio.play()
} else {
audio.pause()
}
})
}
document.addEventListener('DOMContentLoaded', function() {
content = document.querySelector('#content')
songInfo.forEach(function(songData) {
div = document.createElement('div')
div.classList.add('record')
div.dataset.id = songData.id
div.innerHTML = "\
<img class='cover'/>\
<div class='overlay'>\
<div>\
<h1 class='title'></h1>\
<h2 class='album'></h2>\
<h3 class='artist'></h3>\
</div>\
\
<div class='playbar'>\
<div class='timeplayed'></div>\
<div class='timeremaining'></div>\
</div>\
</div>\
<div class='overlay play'>\
<h1 class='playstate'>▶️</h1>\
</div>\
<audio>\
<source src='" + songData.fileLocation + "' type='" + songData.fileType +"'>\
Your browser does not support the audio element.\
</audio>"
div.style = "display: none";
img = div.querySelector('img')
setSongInfo(div, songData);
content.appendChild(div);
img.onload = function(evt) {
evt.target.parentElement.style = "";
}
})
});
</script>
</head>
<body>
<div id="content">
</div>
<footer>
<a href="https://github.com/kuboschek/sop">sop - the simple song page</a>
</footer>
</body>
</html>