-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
209 lines (176 loc) · 6.64 KB
/
script.js
File metadata and controls
209 lines (176 loc) · 6.64 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
console.log('Lets write JavaScript');
let currentSong = new Audio();
let songs = []; // Songs from songs.json (Cloudinary)
let currentIndex = 0;
// Format seconds to mm:ss
function secondsToMinutesSeconds(seconds) {
if (isNaN(seconds) || seconds < 0) return "00:00";
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}
// Fetch songs.json from Cloudinary or your hosting
async function getSongsFromCloudinary(jsonUrl) {
const res = await fetch(jsonUrl);
songs = await res.json();
const songUL = document.querySelector(".songList ul");
songUL.innerHTML = "";
songs.forEach((song, i) => {
songUL.innerHTML += `
<li>
<img class="invert" width="34" src="img/music.svg" alt="">
<div class="info">
<div>${song.title}</div>
<div>Shubham</div>
</div>
<div class="playnow">
<span>Play Now</span>
<img class="invert" src="img/play.svg" alt="">
</div>
</li>
`;
});
// Add click listeners to each <li>
Array.from(songUL.getElementsByTagName("li")).forEach((e, i) => {
e.addEventListener("click", () => {
playMusicFromCloudinary(i);
});
});
}
async function displayPlaylistsFromJSON() {
let res = await fetch("/songs.json");
let playlists = await res.json();
const cardContainer = document.querySelector(".cardContainer");
cardContainer.innerHTML = "";
playlists.forEach((playlist, index) => {
cardContainer.innerHTML += `
<div class="card" data-index="${index}">
<div class="play">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path d="M5 20V4L19 12L5 20Z" stroke="#141B34" fill="#000" stroke-width="1.5"
stroke-linejoin="round" />
</svg>
</div>
<img src="${playlist.cover}" alt="">
<h2>${playlist.name}</h2>
<p>${playlist.description}</p>
</div>`;
});
// On card click → load that playlist
Array.from(document.querySelectorAll(".card")).forEach(card => {
card.addEventListener("click", async e => {
let index = e.currentTarget.dataset.index;
loadPlaylist(index, playlists[index].songs);
});
});
}
function loadPlaylist(index, selectedSongs) {
songs = selectedSongs;
currentIndex = 0;
const songUL = document.querySelector(".songList ul");
songUL.innerHTML = "";
for (const song of songs) {
songUL.innerHTML += `
<li>
<img class="invert" width="34" src="img/music.svg" alt="">
<div class="info">
<div>${song.title}</div>
<div>Shubham</div>
</div>
<div class="playnow">
<span>Play Now</span>
<img class="invert" src="img/play.svg" alt="">
</div>
</li>`;
}
// Attach click to each song
Array.from(songUL.getElementsByTagName("li")).forEach((e, i) => {
e.addEventListener("click", () => {
playMusicFromCloudinary(i);
});
});
playMusicFromCloudinary(0, true); // load first track in pause
}
// Play song from songs array
function playMusicFromCloudinary(index, pause = false) {
currentIndex = index;
const song = songs[index];
currentSong.src = song.url;
if (!pause) {
currentSong.play();
play.src = "img/pause.svg";
}
document.querySelector(".songinfo").innerHTML = decodeURI(song.title);
document.querySelector(".songtime").innerHTML = "00:00 / 00:00";
}
// App logic
async function main() {
await getSongsFromCloudinary("./songs.json"); // Hosted alongside index.html
playMusicFromCloudinary(0, true); // Load first song (paused)
// Play/Pause toggle
play.addEventListener("click", () => {
if (currentSong.paused) {
currentSong.play();
play.src = "img/pause.svg";
} else {
currentSong.pause();
play.src = "img/play.svg";
}
});
// Seekbar update
currentSong.addEventListener("timeupdate", () => {
document.querySelector(".songtime").innerHTML =
`${secondsToMinutesSeconds(currentSong.currentTime)} / ${secondsToMinutesSeconds(currentSong.duration)}`;
document.querySelector(".circle").style.left =
(currentSong.currentTime / currentSong.duration) * 100 + "%";
});
// Seekbar seek
document.querySelector(".seekbar").addEventListener("click", e => {
let percent = (e.offsetX / e.target.getBoundingClientRect().width);
currentSong.currentTime = currentSong.duration * percent;
document.querySelector(".circle").style.left = percent * 100 + "%";
});
// Hamburger open
document.querySelector(".hamburger").addEventListener("click", () => {
document.querySelector(".left").style.left = "0";
});
// Hamburger close
document.querySelector(".close").addEventListener("click", () => {
document.querySelector(".left").style.left = "-120%";
});
// Previous song
previous.addEventListener("click", () => {
if (currentIndex > 0) {
playMusicFromCloudinary(currentIndex - 1);
}
});
// Next song
next.addEventListener("click", () => {
if (currentIndex < songs.length - 1) {
playMusicFromCloudinary(currentIndex + 1);
}
});
// Volume control
document.querySelector(".range input").addEventListener("change", (e) => {
currentSong.volume = parseInt(e.target.value) / 100;
if (currentSong.volume > 0) {
document.querySelector(".volume > img").src = "img/volume.svg";
}
});
// Mute toggle
document.querySelector(".volume > img").addEventListener("click", (e) => {
if (e.target.src.includes("volume.svg")) {
e.target.src = e.target.src.replace("volume.svg", "mute.svg");
currentSong.volume = 0;
document.querySelector(".range input").value = 0;
} else {
e.target.src = e.target.src.replace("mute.svg", "volume.svg");
currentSong.volume = 0.1;
document.querySelector(".range input").value = 10;
}
});
}(async function () {
await displayPlaylistsFromJSON();
main();
})();