Spaces:
Runtime error
Runtime error
Update public/script.js
Browse files- public/script.js +54 -23
public/script.js
CHANGED
|
@@ -4,6 +4,7 @@ let currentPlaying = ''; // Store the filename of the currently playing music
|
|
| 4 |
let trackList = []; // Store currently displayed track filenames
|
| 5 |
let currentPage = 1;
|
| 6 |
let totalPages = 0;
|
|
|
|
| 7 |
|
| 8 |
window.onload = () => {
|
| 9 |
// Attach click handlers for paging
|
|
@@ -17,6 +18,7 @@ window.onload = () => {
|
|
| 17 |
});
|
| 18 |
document.getElementById('audio-player').style.display = 'none';
|
| 19 |
updatePageIndicator();
|
|
|
|
| 20 |
};
|
| 21 |
|
| 22 |
function changePage(direction) {
|
|
@@ -148,6 +150,45 @@ function updatePlayingIndicator() {
|
|
| 148 |
});
|
| 149 |
}
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
function playMusic(fileName) {
|
| 152 |
if (isLoadingMusic && fileName === currentPlaying) {
|
| 153 |
console.log("This song is already loading.");
|
|
@@ -157,33 +198,24 @@ function playMusic(fileName) {
|
|
| 157 |
return;
|
| 158 |
}
|
| 159 |
|
| 160 |
-
isLoadingMusic = true;
|
| 161 |
-
currentPlaying = fileName;
|
| 162 |
|
| 163 |
updatePlayingIndicator();
|
| 164 |
|
| 165 |
-
|
| 166 |
-
const loaderContainer = document.getElementById('loader-container');
|
| 167 |
-
loaderContainer.innerHTML = ''; // Clears any existing loader elements
|
| 168 |
-
|
| 169 |
-
const player = document.getElementById('audio-player');
|
| 170 |
-
const loaderElement = document.createElement('div');
|
| 171 |
-
loaderElement.classList.add('loader');
|
| 172 |
-
loaderContainer.appendChild(loaderElement);
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
-
isLoadingMusic = false;
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
player.play(); // Start playback
|
| 183 |
-
player.loop = isLooping; // Ensure looping state is applied
|
| 184 |
};
|
| 185 |
|
| 186 |
-
|
| 187 |
if (!isLooping) {
|
| 188 |
const currentIndex = trackList.indexOf(currentPlaying);
|
| 189 |
if (currentIndex !== -1 && currentIndex < trackList.length - 1) {
|
|
@@ -192,9 +224,8 @@ function playMusic(fileName) {
|
|
| 192 |
}
|
| 193 |
};
|
| 194 |
|
| 195 |
-
|
| 196 |
-
isLoadingMusic = false;
|
| 197 |
-
loaderContainer.innerHTML = ''; // Clear out the loader element
|
| 198 |
alert(`Error loading the music: ${fileName}`);
|
| 199 |
currentPlaying = ''; // Reset the current playing track
|
| 200 |
};
|
|
|
|
| 4 |
let trackList = []; // Store currently displayed track filenames
|
| 5 |
let currentPage = 1;
|
| 6 |
let totalPages = 0;
|
| 7 |
+
let audio = new Audio();
|
| 8 |
|
| 9 |
window.onload = () => {
|
| 10 |
// Attach click handlers for paging
|
|
|
|
| 18 |
});
|
| 19 |
document.getElementById('audio-player').style.display = 'none';
|
| 20 |
updatePageIndicator();
|
| 21 |
+
setupCustomAudioPlayer();
|
| 22 |
};
|
| 23 |
|
| 24 |
function changePage(direction) {
|
|
|
|
| 150 |
});
|
| 151 |
}
|
| 152 |
|
| 153 |
+
function setupCustomAudioPlayer() {
|
| 154 |
+
// Play/Pause toggle
|
| 155 |
+
document.getElementById('play-pause-btn').addEventListener('click', function() {
|
| 156 |
+
if (audio.paused) {
|
| 157 |
+
audio.play();
|
| 158 |
+
this.textContent = 'Pause'; // Update button text to "Pause"
|
| 159 |
+
} else {
|
| 160 |
+
audio.pause();
|
| 161 |
+
this.textContent = 'Play'; // Update button text to "Play"
|
| 162 |
+
}
|
| 163 |
+
});
|
| 164 |
+
|
| 165 |
+
// Update progress bar as the audio plays
|
| 166 |
+
audio.addEventListener('timeupdate', function() {
|
| 167 |
+
const progress = document.getElementById('progress-bar');
|
| 168 |
+
progress.value = (audio.currentTime / audio.duration) * 100;
|
| 169 |
+
});
|
| 170 |
+
|
| 171 |
+
// Seek in the audio when the progress bar value changes
|
| 172 |
+
document.getElementById('progress-bar').addEventListener('input', function() {
|
| 173 |
+
const duration = audio.duration;
|
| 174 |
+
audio.currentTime = (this.value * duration) / 100;
|
| 175 |
+
});
|
| 176 |
+
|
| 177 |
+
// Mute toggle
|
| 178 |
+
document.getElementById('mute-btn').addEventListener('click', function() {
|
| 179 |
+
audio.muted = !audio.muted;
|
| 180 |
+
this.textContent = audio.muted ? 'Unmute' : 'Mute'; // Update button text based on mute state
|
| 181 |
+
});
|
| 182 |
+
|
| 183 |
+
// Volume control
|
| 184 |
+
document.getElementById('volume-control').addEventListener('input', function() {
|
| 185 |
+
audio.volume = this.value / 100;
|
| 186 |
+
});
|
| 187 |
+
|
| 188 |
+
// Initial volume (100%)
|
| 189 |
+
audio.volume = 1.0;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
function playMusic(fileName) {
|
| 193 |
if (isLoadingMusic && fileName === currentPlaying) {
|
| 194 |
console.log("This song is already loading.");
|
|
|
|
| 198 |
return;
|
| 199 |
}
|
| 200 |
|
| 201 |
+
isLoadingMusic = true;
|
| 202 |
+
currentPlaying = fileName;
|
| 203 |
|
| 204 |
updatePlayingIndicator();
|
| 205 |
|
| 206 |
+
document.getElementById('audio-player').style.display = 'flex'; // Show custom player UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
// Set the new source file for audio
|
| 209 |
+
audio.src = `/music/${fileName}`;
|
| 210 |
+
audio.load(); // Start loading the new source file
|
| 211 |
|
| 212 |
+
audio.oncanplaythrough = () => {
|
| 213 |
+
isLoadingMusic = false;
|
| 214 |
+
document.getElementById('play-pause-btn').textContent = 'Pause'; // Update play/pause button
|
| 215 |
+
audio.play(); // Start playback
|
|
|
|
|
|
|
| 216 |
};
|
| 217 |
|
| 218 |
+
audio.onended = () => {
|
| 219 |
if (!isLooping) {
|
| 220 |
const currentIndex = trackList.indexOf(currentPlaying);
|
| 221 |
if (currentIndex !== -1 && currentIndex < trackList.length - 1) {
|
|
|
|
| 224 |
}
|
| 225 |
};
|
| 226 |
|
| 227 |
+
audio.onerror = () => {
|
| 228 |
+
isLoadingMusic = false;
|
|
|
|
| 229 |
alert(`Error loading the music: ${fileName}`);
|
| 230 |
currentPlaying = ''; // Reset the current playing track
|
| 231 |
};
|