Spaces:
Runtime error
Runtime error
Update public/script.js
Browse files- public/script.js +57 -60
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
|
|
@@ -148,7 +149,9 @@ function updatePlayingIndicator() {
|
|
| 148 |
});
|
| 149 |
}
|
| 150 |
|
| 151 |
-
|
|
|
|
|
|
|
| 152 |
function playMusic(fileName) {
|
| 153 |
if (isLoadingMusic && fileName === currentPlaying) {
|
| 154 |
console.log("This song is already loading.");
|
|
@@ -158,84 +161,78 @@ function playMusic(fileName) {
|
|
| 158 |
return;
|
| 159 |
}
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
isLoadingMusic = true;
|
| 164 |
-
currentPlaying = fileName;
|
| 165 |
|
| 166 |
updatePlayingIndicator();
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
player.loop = isLooping;
|
| 179 |
-
updateDuration();
|
| 180 |
-
document.getElementById('play-pause-btn').textContent = 'Pause'; // Update button label
|
| 181 |
};
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
document.getElementById('loader-container').innerHTML = ''; // Clear out the loader element
|
| 187 |
-
alert(`Error loading the music: ${fileName}`);
|
| 188 |
-
currentPlaying = '';
|
| 189 |
};
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
};
|
| 195 |
}
|
| 196 |
|
| 197 |
-
//
|
| 198 |
-
|
| 199 |
-
|
|
|
|
| 200 |
const playPauseBtn = document.getElementById('play-pause-btn');
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
player.play();
|
| 204 |
playPauseBtn.textContent = 'Pause';
|
| 205 |
} else {
|
| 206 |
-
|
| 207 |
playPauseBtn.textContent = 'Play';
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
-
//
|
| 212 |
-
function
|
| 213 |
-
const
|
| 214 |
-
document.getElementById('
|
| 215 |
-
document.getElementById('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
}
|
| 217 |
|
|
|
|
| 218 |
function formatTime(seconds) {
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
sec = `0${sec}`;
|
| 223 |
-
}
|
| 224 |
-
return `${min}:${sec}`;
|
| 225 |
}
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
document.getElementById('audio-player').addEventListener('timeupdate', () => {
|
| 231 |
-
const player = document.getElementById('audio-player');
|
| 232 |
-
document.getElementById('seek-bar').value = player.currentTime;
|
| 233 |
-
document.getElementById('current-time').textContent = formatTime(player.currentTime);
|
| 234 |
-
});
|
| 235 |
-
|
| 236 |
-
document.getElementById('seek-bar').addEventListener('input', (e) => {
|
| 237 |
-
const player = document.getElementById('audio-player');
|
| 238 |
-
player.currentTime = e.target.value;
|
| 239 |
-
});
|
| 240 |
-
|
| 241 |
-
// Add listeners for other controls as needed (e.g., next, previous)
|
|
|
|
| 4 |
let trackList = []; // Store currently displayed track filenames
|
| 5 |
let currentPage = 1;
|
| 6 |
let totalPages = 0;
|
| 7 |
+
let audioElement = new Audio();
|
| 8 |
|
| 9 |
window.onload = () => {
|
| 10 |
// Attach click handlers for paging
|
|
|
|
| 149 |
});
|
| 150 |
}
|
| 151 |
|
| 152 |
+
let audioElement = new Audio(); // Use the Audio constructor for creating the audio element
|
| 153 |
+
|
| 154 |
+
// Replace or modify the `playMusic` function to manage the custom player:
|
| 155 |
function playMusic(fileName) {
|
| 156 |
if (isLoadingMusic && fileName === currentPlaying) {
|
| 157 |
console.log("This song is already loading.");
|
|
|
|
| 161 |
return;
|
| 162 |
}
|
| 163 |
|
| 164 |
+
isLoadingMusic = true; // Moved here to handle the loading state correctly for a new song.
|
| 165 |
+
currentPlaying = fileName; // Update the currentPlaying variable to the new song.
|
|
|
|
|
|
|
| 166 |
|
| 167 |
updatePlayingIndicator();
|
| 168 |
|
| 169 |
+
// Hide the loader if used
|
| 170 |
+
document.getElementById('loader-container').innerHTML = ''; // Clears any existing loader elements
|
| 171 |
+
|
| 172 |
+
audioElement.src = `/music/${fileName}`; // Set the new source file
|
| 173 |
+
audioElement.load(); // Start loading the new source file
|
| 174 |
+
handlePlayPause(); // Play the loaded track and update UI accordingly
|
| 175 |
+
|
| 176 |
+
audioElement.oncanplaythrough = () => {
|
| 177 |
+
isLoadingMusic = false; // Update the loading flag when ready to play
|
| 178 |
+
handlePlayPause(true); // Play the loaded track and update UI accordingly
|
|
|
|
|
|
|
|
|
|
| 179 |
};
|
| 180 |
|
| 181 |
+
audioElement.ontimeupdate = () => {
|
| 182 |
+
// Update the progress bar and time labels
|
| 183 |
+
updateProgress();
|
|
|
|
|
|
|
|
|
|
| 184 |
};
|
| 185 |
|
| 186 |
+
audioElement.onended = () => {
|
| 187 |
+
if (!isLooping) {
|
| 188 |
+
const currentIndex = trackList.indexOf(currentPlaying);
|
| 189 |
+
if (currentIndex !== -1 && currentIndex < trackList.length - 1) {
|
| 190 |
+
playMusic(trackList[currentIndex + 1]);
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
};
|
| 194 |
+
|
| 195 |
+
audioElement.onerror = () => {
|
| 196 |
+
isLoadingMusic = false; // Ensure flag is reset on error
|
| 197 |
+
alert(`Error loading the music: ${fileName}`);
|
| 198 |
+
currentPlaying = ''; // Reset the current playing track
|
| 199 |
};
|
| 200 |
}
|
| 201 |
|
| 202 |
+
// Adapt or integrate the `toggleLoop` and other utility functions as needed.
|
| 203 |
+
|
| 204 |
+
// Function to handle play/pause actions and update UI.
|
| 205 |
+
function handlePlayPause(shouldPlay = false) {
|
| 206 |
const playPauseBtn = document.getElementById('play-pause-btn');
|
| 207 |
+
if (shouldPlay || audioElement.paused) {
|
| 208 |
+
audioElement.play();
|
|
|
|
| 209 |
playPauseBtn.textContent = 'Pause';
|
| 210 |
} else {
|
| 211 |
+
audioElement.pause();
|
| 212 |
playPauseBtn.textContent = 'Play';
|
| 213 |
}
|
| 214 |
}
|
| 215 |
|
| 216 |
+
// Function to update progress bar and time labels.
|
| 217 |
+
function updateProgress() {
|
| 218 |
+
const progress = document.getElementById('progress');
|
| 219 |
+
const currentTime = document.getElementById('current-time');
|
| 220 |
+
const duration = document.getElementById('duration');
|
| 221 |
+
|
| 222 |
+
const progressPercent = (audioElement.currentTime / audioElement.duration) * 100;
|
| 223 |
+
progress.style.width = `${progressPercent}%`;
|
| 224 |
+
|
| 225 |
+
currentTime.textContent = formatTime(audioElement.currentTime);
|
| 226 |
+
duration.textContent = formatTime(audioElement.duration);
|
| 227 |
}
|
| 228 |
|
| 229 |
+
// Utility function to format time from seconds to M:SS
|
| 230 |
function formatTime(seconds) {
|
| 231 |
+
const mins = Math.floor(seconds / 60);
|
| 232 |
+
const secs = Math.floor(seconds % 60).toString().padStart(2, '0');
|
| 233 |
+
return `${mins}:${secs}`;
|
|
|
|
|
|
|
|
|
|
| 234 |
}
|
| 235 |
|
| 236 |
+
document.getElementById('play-pause-btn').addEventListener('click', () => {
|
| 237 |
+
handlePlayPause();
|
| 238 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|