-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic-box.js
224 lines (214 loc) · 6.88 KB
/
music-box.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
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
let songs = [
{
"title": "Drain You",
"artist": "Nirvana",
"file": "drain_you.m4a",
"cover": "nevermind.jpg"
},
{
"title": "Brand New City",
"artist": "Mitski",
"file": "brand_new_city.m4a",
"cover": "mitski_lush.jpeg"
},
{
"title": "Too Close",
"artist": "Sir Cloe",
"file": "too_close.m4a",
"cover": "too-close.jpeg"
},
{
"title": "it's ok, you're ok",
"artist": "Bonjr",
"file": "its-ok-youre-ok.m4a",
"cover": "bonjr.jpg"
},
{
"title": "This Is Home",
"artist": "Cavetown",
"file": "this-is-home.m4a",
"cover": "this-is-home.jpeg"
},
{
"title": "Where Is My Mind?",
"artist": "Pixies",
"file": "where-is-my-mind.m4a",
"cover": "death-to-the-pixies.jpg"
}
];
var nowPlaying = Math.floor(Math.random() * songs.length);
var volumeSliderShown = false;
var isPaused = true;
var audio = new Audio(songs[nowPlaying].file);
var duration = 0;
function initMusicPlayer() {
const playerDiv = document.getElementById('player');
playerDiv.innerHTML = `
<div class="screen">
<img src="" width="120px" height="120px" alt="album cover" id="cover">
<div class="screenbox">
<div class="titlebox">
<p><span id="title">brand new city</span><br><span id="artist">mitski</span></p>
</div>
<div class="progressbar">
<label for="progress" id="timestamp">0:00</label>
<input type="range" min="0" max="100" value="0" class="slider" id="progress">
<label for="progress" id="duration">0:00</label>
</div>
</div>
</div>
<div class="buttons">
<button onclick="previousSong()">previous</button>
<button onclick="playPause()" id="playButton">play</button>
<button onclick="nextSong()">next</button>
<button onclick="showVolume()">volume</button>
<div id="volumebar">
<input type="range" min="0" max="100" value="100" class="slider" id="volume">
</div>
</div>
`;
// Apply the CSS styles
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `
#player {
border: 1px solid black;
max-width: fit-content;
}
.screen {
display: flex;
}
#cover {
margin: 1px;
border: 1px solid black;
}
.titlebox {
padding-left: 5px;
}
.progressbar {
margin: 0 5px 0 5px;
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-items: center;
}
.title {
font-size: larger;
font-weight: 600;
}
.screenbox {
display: flex;
flex-flow: column nowrap;
justify-content: space-between;
}
#volume {
width: 100px;
}
#volumebar {
visibility: hidden;
display: flex;
flex-direction: row-reverse;
}
.buttons {
display: flex;
flex-flow: row;
justify-content: space-between;
}
`;
document.head.appendChild(style);
}
// Call the function to initialize the music player
initMusicPlayer();
volume.addEventListener("input", function(){
let volume = parseInt(this.value)/100;
audio.volume = volume;
})
progress.addEventListener("input", function () {
let seekTime = parseInt(this.value);
audio.currentTime = seekTime;
// Update the timestamp here, similar to what you do in the timeupdate event
let current_minutes = Math.floor(seekTime / 60);
let current_seconds = Math.floor(seekTime % 60);
if (current_seconds < 10) {
current_seconds = "0" + String(current_seconds);
}
let formatted_time = String(current_minutes) + ":" + String(current_seconds);
document.getElementById("timestamp").innerHTML = formatted_time;
});
function updateProgressBar(currentTime) {
// Update the progress bar directly
document.getElementById("progress").value = Math.floor(currentTime);
if(currentTime >= Math.floor(audio.duration)-0.1 && !isPaused){
nextSong();
}
// Update the timestamp
let current_minutes = Math.floor(currentTime / 60);
let current_seconds = Math.floor(currentTime % 60);
if (current_seconds < 10) {
current_seconds = "0" + String(current_seconds);
}
let formatted_time = String(current_minutes) + ":" + String(current_seconds);
document.getElementById("timestamp").innerHTML = formatted_time;
}
loadSong(nowPlaying);
audio.addEventListener("timeupdate", function () {
let currentTime = audio.currentTime; // Current playback time in seconds
updateProgressBar(currentTime);
});
function loadSong(number) {
let cover = document.getElementById("cover");
cover.setAttribute("src", "covers/" + songs[number].cover);
let artist = document.getElementById("artist");
artist.innerHTML = songs[number].artist;
let title = document.getElementById("title");
title.innerHTML = songs[number].title;
audio.src = "songs/"+songs[number].file;
audio.addEventListener("loadedmetadata", function () {
duration = audio.duration; // Duration of the loaded song in seconds
// Set the maximum value for the progress bar
document.getElementById("progress").max = Math.floor(duration);
// Update the duration
let duration_minutes = Math.floor(duration / 60);
let duration_seconds = Math.floor(duration % 60);
if (duration_seconds < 10) {
duration_seconds = "0" + String(duration_seconds);
}
let formatted_duration = String(duration_minutes) + ":" + String(duration_seconds);
document.getElementById("duration").innerHTML = formatted_duration;
});
}
function nextSong() {
nowPlaying = (nowPlaying + 1) % songs.length;
loadSong(nowPlaying);
if (!isPaused) {
audio.play();
}
}
function playPause() {
if (isPaused) {
document.getElementById("playButton").innerHTML = "pause";
audio.play();
} else {
document.getElementById("playButton").innerHTML = "play";
audio.pause();
}
isPaused = !isPaused;
}
function previousSong() {
nowPlaying--;
if (nowPlaying < 0) {
nowPlaying = songs.length - 1;
}
loadSong(nowPlaying);
if (!isPaused) {
audio.play();
}
}
function showVolume() {
if (volumeSliderShown) {
document.getElementById("volumebar").style.visibility = "hidden";
} else {
document.getElementById("volumebar").style.visibility = "visible";
}
volumeSliderShown = !volumeSliderShown;
}