-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
291 lines (242 loc) · 9.21 KB
/
script.js
File metadata and controls
291 lines (242 loc) · 9.21 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
function toggleSearch() {
const searchContainer = document.getElementById('searchContainer');
const searchBtn = document.querySelector('.search');
if (!searchContainer.classList.contains('open')) {
// Opening the search
searchContainer.style.display = 'flex';
// Add the open class after a tiny delay to trigger transition
setTimeout(() => {
searchContainer.classList.add('open');
}, 5);
// Hide search button with fade
searchBtn.style.opacity = '0';
setTimeout(() => {
searchBtn.style.display = 'none';
}, 150);
// Focus on input after animation
setTimeout(() => {
searchContainer.querySelector('.search-input').focus();
}, 100);
} else {
closeSearch();
}
}
function closeSearch() {
const searchContainer = document.getElementById('searchContainer');
const searchBtn = document.querySelector('.search');
// Remove open class to trigger closing animation
searchContainer.classList.remove('open');
// Show search button with fade in
setTimeout(() => {
searchBtn.style.display = 'flex';
searchBtn.style.opacity = '1';
}, 100);
// Hide container after animation completes
setTimeout(() => {
searchContainer.style.display = 'none';
// Clear the search input
searchContainer.querySelector('.search-input').value = '';
}, 250);
}
function handleSearch(event) {
const searchTerm = event.target.value;
if (event.key === 'Enter' && searchTerm.trim()) {
console.log('Searching for:', searchTerm);
}
if (event.key === 'Escape') {
closeSearch();
}
}
console.log("lets write javascript")
let currentAudio = null;
let currentSongIndex = 0;
let allSongs = [];
let isDragging = false;
let isVolumeDragging = false; // ✅ Added volume dragging variable
async function getSongs() {
try {
let a = await fetch("http://127.0.0.1:5500/songs/")
let response = await a.text();
let hrefs = response.match(/href="[^"]*\.mp3"/g);
if (hrefs) {
// Convert to full HTTP URLs
let songUrls = hrefs.map(href => {
let path = href.replace('href="', '').replace('"', '');
return path;
});
console.log("Found songs:", songUrls);
return songUrls;
} else {
console.log("No .mp3 files found");
return [];
}
} catch (error) {
console.error("Error fetching songs:", error);
return [];
}
}
const playMusic = (fileName, pause = false) => {
if (currentAudio) {
currentAudio.pause();
currentAudio.currentTime = 0;
}
currentAudio = new Audio(fileName)
currentAudio.volume = 0.7;
console.log(fileName)
if (!pause) {
currentAudio.play();
}
fileName = decodeURIComponent(fileName);
document.querySelector(".songinfo").innerHTML = fileName.split("mx-")[1]
currentAudio.addEventListener('timeupdate', () => {
const currentTime = currentAudio.currentTime;
const duration = currentAudio.duration;
document.querySelector('.time-display').textContent = formatTime(currentTime);
// Update progress bar
if (duration) {
const progressPercent = (currentTime / duration) * 100;
document.querySelector('.progress-fill').style.width = progressPercent + '%';
}
});
currentAudio.addEventListener('loadedmetadata', () => {
const duration = currentAudio.duration;
document.querySelectorAll('.time-display')[1].textContent = formatTime(duration);
});
}
function updateSeekPosition(e) {
const progressTrack = document.querySelector(".progress-track");
const rect = progressTrack.getBoundingClientRect();
// Calculate position
const clickX = e.clientX - rect.left;
const trackWidth = progressTrack.offsetWidth;
// Keep within bounds
const boundedX = Math.max(0, Math.min(clickX, trackWidth));
const clickPercent = (boundedX / trackWidth) * 100;
const seekTime = (clickPercent / 100) * currentAudio.duration;
// Update audio and visual
currentAudio.currentTime = seekTime;
document.querySelector('.progress-fill').style.width = clickPercent + '%';
}
// ✅ Added volume dragging function
function updateVolumePosition(e) {
const volumeBar = document.querySelector(".volume-bar");
const rect = volumeBar.getBoundingClientRect();
// Calculate position
const clickX = e.clientX - rect.left;
const barWidth = volumeBar.offsetWidth;
// Keep within bounds
const boundedX = Math.max(0, Math.min(clickX, barWidth));
const volumePercent = (boundedX / barWidth) * 100;
const volumeLevel = volumePercent / 100;
// Update audio volume
if (currentAudio) {
currentAudio.volume = volumeLevel;
}
// Update visual volume bar
document.querySelector('.volume-fill').style.width = volumePercent + '%';
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
}
function setVolume(e) {
const volumeBar = document.querySelector(".volume-bar");
const rect = volumeBar.getBoundingClientRect();
const clickX = e.clientX - rect.left;
const barWidth = volumeBar.offsetWidth;
const volumePercent = (clickX / barWidth) * 100;
// Set audio volume
if (currentAudio) {
currentAudio.volume = volumePercent / 100;
}
// Update visual bar
document.querySelector('.volume-fill').style.width = volumePercent + '%';
}
async function main() {
let currentSong;
console.log("Starting main function...");
let songs = await getSongs();
allSongs = songs;
playMusic(allSongs[0], true);
let songUL = document.querySelector(".songlist ul");
songUL.innerHTML = "";
for (const song of songs) {
if (typeof song !== "string") continue;
let cleanSongName = song.replaceAll("%20", " ").replaceAll("%E2%80%93", "-").replaceAll(".mp3", "").split("mx-")[1];
// Updated structure with left-section wrapper
songUL.innerHTML = songUL.innerHTML + `
<li data-song="${song}">
<div class="left-section">
<img src="music.svg" alt="" class="invert">
<div class="info">
<div class="musicinfo">${cleanSongName}</div>
<div class="artist">Mohit</div>
</div>
</div>
<div class="playnow">
<span>Play Now</span>
<img src="playlist.svg" alt="" class="invert">
</div>
</li>`;
}
//an event lstner to each song
Array.from(document.querySelector(".songlist").getElementsByTagName("li")).forEach(e => {
e.addEventListener("click", element => {
let songFileName = e.getAttribute("data-song");
currentSongIndex = allSongs.indexOf(songFileName);
playMusic(`http://127.0.0.1:5500/${songFileName}`);
});
});
play.addEventListener("click", () => {
if (currentAudio.paused) {
currentAudio.play();
document.getElementById("play").src = "pause.svg";
} else {
currentAudio.pause();
document.getElementById("play").src = "playlist.svg";
}
});
next.addEventListener("click", () => {
currentSongIndex = (currentSongIndex + 1) % allSongs.length;
playMusic(`http://127.0.0.1:5500/${allSongs[currentSongIndex]}`)
});
previous.addEventListener("click", () => {
currentSongIndex = (currentSongIndex - 1 + allSongs.length) % allSongs.length;
playMusic(`http://127.0.0.1:5500/${allSongs[currentSongIndex]}`)
});
const progressTrack = document.querySelector(".progress-track");
// Start dragging
progressTrack.addEventListener("mousedown", (e) => {
if (!currentAudio || !currentAudio.duration) return;
isDragging = true;
updateSeekPosition(e);
});
// While dragging
document.addEventListener("mousemove", (e) => {
if (!isDragging && !isVolumeDragging) return;
if (isDragging) updateSeekPosition(e);
if (isVolumeDragging) updateVolumePosition(e); // ✅ Added volume dragging
});
// Stop dragging
document.addEventListener("mouseup", () => {
isDragging = false;
isVolumeDragging = false; // ✅ Added volume dragging reset
});
document.querySelector(".hamburger").addEventListener("click", () => {
document.querySelector(".left").style.left = "0"
})
// ✅ Fixed: Removed duplicate close listener
document.querySelector(".close").addEventListener("click", () => {
document.querySelector(".left").style.left = "-120%"
})
// Volume control - click
document.querySelector(".volume-bar").addEventListener("click", setVolume);
// ✅ Added volume dragging events
const volumeBar = document.querySelector(".volume-bar");
volumeBar.addEventListener("mousedown", (e) => {
isVolumeDragging = true;
updateVolumePosition(e);
});
}
main()