Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (125 loc) · 4.94 KB
/
Copy pathscript.js
File metadata and controls
151 lines (125 loc) · 4.94 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
let currentSong = new Audio();
let songs;
let playingSongs;
let currentIndex = 0;
function formatTime(seconds) {
let minutes = Math.floor(seconds / 60);
let secs = Math.floor(seconds % 60);
if (secs < 10) secs = "0" + secs;
return minutes + ":" + secs;
}
async function getSongs(folder) {
try {
const response = await fetch("songs/songs.json");
const data = await response.json();
songs = data[folder];
const songul = document.querySelector(".songlist ul");
songul.innerHTML = "";
// show songs in the li
for (const song of songs) {
const songName = song.split("/").pop().replace(".mp3", "");
songul.innerHTML += `
<li data-track="${song}">
<img class="invert" src="img/music.svg">
<div class="info">${decodeURIComponent(songName)}</div>
<img src="img/mplay.svg">
</li>`;
}
songul.onclick = function (e) {
const li = e.target.closest("li");
if (!li) return;
const track = li.dataset.track;
if (track) {
playMusic(track);
document.querySelector(".btnplay").src = "img/pause.svg";
}
};
} catch (err) {
console.error("Error loading songs:", err);
}
}
const playMusic = (track, pause = false) => {
currentSong.src = track;
currentIndex = songs.indexOf(track);
playingSongs = songs;
if (!pause) currentSong.play().catch(() => { });
const songName = track.split("/").pop().replace(".mp3", "");
document.querySelector(".songinfo").innerHTML = decodeURIComponent(songName);
document.querySelector(".songtime").innerHTML = "00:00 / 00:00";
};
async function main() {
await getSongs("coke");
playMusic(songs[0], true);
// change the play and pause svg when clicked
const play = document.querySelector(".btnplay");
play.addEventListener("click", () => {
if (currentSong.paused) {
currentSong.play().catch(() => { });
play.src = "img/pause.svg";
} else {
currentSong.pause();
play.src = "img/btnplay.svg";
}
});
//change the time while song is playing
currentSong.addEventListener("timeupdate", () => {
if (!isNaN(currentSong.duration)) {
document.querySelector(".songtime").innerHTML =
`${formatTime(currentSong.currentTime)} / ${formatTime(currentSong.duration)}`;
document.querySelector(".circle").style.left =
(currentSong.currentTime / currentSong.duration) * 100 + "%";
}
});
document.querySelector(".seekbar").addEventListener("click", e => {
const percent = (e.offsetX / e.target.getBoundingClientRect().width) * 100;
document.querySelector(".circle").style.left = percent + "%";
currentSong.currentTime = (currentSong.duration * percent) / 100;
});
document.querySelector(".ham").addEventListener("click", () => {
document.querySelector(".left").style.left = "0";
});
document.querySelector(".close").addEventListener("click", () => {
document.querySelector(".left").style.left = "-100%";
});
//add eventlistener to prev and next button
document.querySelector(".prev").addEventListener("click", () => {
if (currentIndex > 0) {
songs = playingSongs;
playMusic(playingSongs[currentIndex - 1]);
play.src = "img/pause.svg";
}
});
document.querySelector(".next").addEventListener("click", () => {
if (currentIndex < playingSongs.length - 1) {
songs = playingSongs;
playMusic(playingSongs[currentIndex + 1]);
play.src = "img/pause.svg";
}
});
document.querySelector(".inp").addEventListener("change", (e) => {
currentSong.volume = parseInt(e.target.value) / 100;
});
Array.from(document.getElementsByClassName("card")).forEach(card => {
card.addEventListener("click", async () => {
const folderKey = card.dataset.folder;
await getSongs(folderKey);
// no autoplay — current song keeps playing
document.querySelector(".left").style.left = "0";
});
});
document.querySelector(".volume>img").addEventListener("click", e => {
if (e.target.src.includes("img/volume.svg")) {
e.target.src = e.target.src.replace("img/volume.svg", "img/mute.svg");
currentSong.volume = 0;
document.querySelector(".volume input").value = 0;
} else {
e.target.src = e.target.src.replace("img/mute.svg", "img/volume.svg");
currentSong.volume = 0.20;
document.querySelector(".volume input").value = 10;
}
});
document.querySelector(".inp").addEventListener("click", () => {
document.querySelector(".volume>img").src = "img/volume.svg";
});
}
main();