-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (152 loc) · 6.33 KB
/
Copy pathscript.js
File metadata and controls
191 lines (152 loc) · 6.33 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
let audio = new Audio();
let currentSongIndex = 0;
let songs = [];
async function getSongs() {
let response = await fetch("http://127.0.0.1:3000/songs");
let textResponse = await response.text();
let div = document.createElement("div");
div.innerHTML = textResponse;
let as = div.getElementsByTagName("a");
for (let index = 0; index < as.length; index++) {
const element = as[index];
if (element.href.endsWith(".mp3")) {
songs.push(element.href);
}
}
return songs;
}
function playSong(index) {
if (index < 0 || index >= songs.length) return;
audio.src = songs[index];
audio.play();
currentSongIndex = index;
updateInfoBar();
updateSeekBar();
playPauseIcon.innerHTML = `<img src="icons/pause.svg" alt="Pause">`;
let playBar = document.getElementsByClassName("playbar")[0]
playBar.style.backgroundColor = "#123319";
audio.onended = () => {
playSong(currentSongIndex + 1); // Automatically play the next song
};
const albumArt = document.getElementById("albumart");
audio.onpause = () => {
albumArt.classList.remove("album-art"); // Remove the rotation class when paused
};
audio.onplay= () => {
albumArt.classList.add("album-art");
}
audio.onended = () => {
albumArt.classList.remove("album-art"); // Remove the rotation class when song ends
};
}
function updateInfoBar() {
const infoBar = document.querySelector('.infobar');
const songName = songs[currentSongIndex].split("songs/")[1].split("-")[1].replaceAll("%20", " ").replaceAll(".mp3", "").replaceAll("0", "");
const artistName = songs[currentSongIndex].split("songs/")[1].split("-")[0].replaceAll("%20", " ").replaceAll(".mp3", "").replaceAll("0", "").replace(/\d+/g, "").replace(".", "");
infoBar.innerHTML = `<div>${songName}</div>
<div>${artistName}</div>`;
}
function updateSeekBar() {
const seekbar = document.getElementById("seekbar");
seekbar.value = 0;
seekbar.max = Math.floor(audio.duration);
const timer = document.querySelector('.timer');
audio.onloadedmetadata = function () {
seekbar.max = Math.floor(audio.duration); // Set the max value of the seekbar to the audio's total duration
};
audio.ontimeupdate = function () {
seekbar.value = Math.floor(audio.currentTime);
timer.textContent = `${formatTime(audio.currentTime)}/${formatTime(audio.duration)}`;
};
seekbar.oninput = function () {
audio.currentTime = seekbar.value;
};
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${minutes}:${secs < 10 ? '0' : ''}${secs}`;
}
async function main() {
songs = await getSongs();
console.log(songs);
const songList = document.getElementsByClassName("playlist")[0];
songs.forEach((song, index) => {
songList.insertAdjacentHTML("beforeend", `<div class="song" data-index="${index}">
<div class="name">
<div class="songinfo">${song.split("songs/")[1].split("-")[1].replaceAll("%20", " ").replaceAll(".mp3", "").replaceAll("0", "")}</div>
<div class="artist">${song.split("songs/")[1].split("-")[0].replaceAll("%20", " ").replaceAll(".mp3", "").replaceAll("0", "").replace(/\d+/g, "").replace(".", "")}</div>
</div>
<div class="playico"><img src="icons/play.svg" alt=""></div>
</div>`);
});
// Attach click event to each song
const songElements = document.querySelectorAll('.song');
songElements.forEach(songElement => {
songElement.addEventListener('click', () => {
const index = parseInt(songElement.dataset.index);
playSong(index);
playPauseIcon.innerHTML = `<img src="icons/pause.svg" alt="Play">`;
});
});
}
let playPauseIcon = document.getElementById("playPause");
// Play/pause control
document.querySelector('.controls .play').addEventListener('click', () => {
if (audio.paused) {
audio.play();
playPauseIcon.innerHTML = `<img src="icons/pause.svg" alt="Pause">`; // Use correct template literal syntax
} else {
audio.pause();
playPauseIcon.innerHTML = `<img src="icons/play.svg" alt="Play">`; // Use correct template literal syntax
}
});
document.querySelector('.controls .previous').addEventListener('click', () => {
playSong(currentSongIndex - 1)
});
document.querySelector('.controls .next').addEventListener('click', () => {
playSong(currentSongIndex + 1)
});
const volumeControl = document.getElementById("volume");
// Set initial volume
audio.volume = volumeControl.value;
// Add event listener for volume change
volumeControl.addEventListener('input', () => {
audio.volume = volumeControl.value; // Set audio volume
});
main();
let menu = document.getElementById("menu")
menu.addEventListener('click', () => {
let library = document.getElementsByClassName("library")[0]
if (library.style.left === "0px") {
library.style.left = "-200%"; // Close menu if open
} else {
library.style.left = "0"; // Open menu if closed
}
event.stopPropagation();
});
let library = document.getElementsByClassName("library")[0]
let body= document.getElementsByTagName("body")[0]
body.addEventListener('click', () => {
if (library.style.left === "0px") {
library.style.left = "-200%"; // Close menu if open
}
});
let cross = document.getElementById("cross")
cross.addEventListener('click', () => {
let library = document.getElementsByClassName("library")[0]
library.style.left = "-200%"
});
let fullplayerico =document.getElementsByClassName("fullplayerico")[0]
let fpi =document.getElementById("fpi")
fullplayerico.addEventListener('click', () => {
let albumdetails = document.getElementsByClassName("albumdetails")[0]
if (albumdetails.style.display === "none") {
albumdetails.style.display = "flex";
albumdetails.style.bottom = "14vh";
fpi.style.transform="rotate(90deg)"
} else {
albumdetails.style.display = "none";
fpi.style.transform="rotate(-90deg)" // Open menu if closed
}
});