Spaces:
Runtime error
Runtime error
Update public/script.js
Browse files- public/script.js +23 -17
public/script.js
CHANGED
|
@@ -129,31 +129,38 @@ function updatePlayingIndicator() {
|
|
| 129 |
}
|
| 130 |
|
| 131 |
function playMusic(fileName) {
|
| 132 |
-
if (isLoadingMusic
|
| 133 |
-
console.log("This song is already loading
|
|
|
|
|
|
|
|
|
|
| 134 |
return;
|
| 135 |
}
|
| 136 |
|
| 137 |
-
isLoadingMusic = true;
|
| 138 |
-
currentPlaying = fileName;
|
|
|
|
| 139 |
updatePlayingIndicator();
|
| 140 |
|
| 141 |
-
|
| 142 |
const loaderContainer = document.getElementById('loader-container');
|
|
|
|
|
|
|
|
|
|
| 143 |
const loaderElement = document.createElement('div');
|
| 144 |
loaderElement.classList.add('loader');
|
| 145 |
loaderContainer.appendChild(loaderElement);
|
| 146 |
|
| 147 |
-
player.style.display = 'none';
|
| 148 |
-
player.src = `/music/${fileName}`;
|
| 149 |
-
player.load();
|
| 150 |
|
| 151 |
player.oncanplaythrough = () => {
|
| 152 |
-
isLoadingMusic = false;
|
| 153 |
-
loaderContainer.
|
| 154 |
-
player.style.display = '';
|
| 155 |
-
player.play();
|
| 156 |
-
player.loop = isLooping;
|
| 157 |
};
|
| 158 |
|
| 159 |
player.onended = () => {
|
|
@@ -161,15 +168,14 @@ function playMusic(fileName) {
|
|
| 161 |
const currentIndex = trackList.indexOf(currentPlaying);
|
| 162 |
if (currentIndex !== -1 && currentIndex < trackList.length - 1) {
|
| 163 |
playMusic(trackList[currentIndex + 1]);
|
| 164 |
-
} else {
|
| 165 |
}
|
| 166 |
}
|
| 167 |
};
|
| 168 |
|
| 169 |
player.onerror = () => {
|
| 170 |
-
isLoadingMusic = false;
|
| 171 |
-
loaderContainer.
|
| 172 |
alert(`Error loading the music: ${fileName}`);
|
| 173 |
-
currentPlaying = '';
|
| 174 |
};
|
| 175 |
}
|
|
|
|
| 129 |
}
|
| 130 |
|
| 131 |
function playMusic(fileName) {
|
| 132 |
+
if (isLoadingMusic && fileName === currentPlaying) {
|
| 133 |
+
console.log("This song is already loading.");
|
| 134 |
+
return;
|
| 135 |
+
} else if (!isLoadingMusic && fileName === currentPlaying) {
|
| 136 |
+
console.log("This song is already playing.");
|
| 137 |
return;
|
| 138 |
}
|
| 139 |
|
| 140 |
+
isLoadingMusic = true; // Moved here to handle the loading state correctly for a new song.
|
| 141 |
+
currentPlaying = fileName; // Update the currentPlaying variable to the new song.
|
| 142 |
+
|
| 143 |
updatePlayingIndicator();
|
| 144 |
|
| 145 |
+
// Previous song's loading process cleanup, if any
|
| 146 |
const loaderContainer = document.getElementById('loader-container');
|
| 147 |
+
loaderContainer.innerHTML = ''; // Clears any existing loader elements
|
| 148 |
+
|
| 149 |
+
const player = document.getElementById('audio-player');
|
| 150 |
const loaderElement = document.createElement('div');
|
| 151 |
loaderElement.classList.add('loader');
|
| 152 |
loaderContainer.appendChild(loaderElement);
|
| 153 |
|
| 154 |
+
player.style.display = 'none'; // Hide the player UI during loading
|
| 155 |
+
player.src = `/music/${fileName}`; // Set the new source file
|
| 156 |
+
player.load(); // Start loading the new source file
|
| 157 |
|
| 158 |
player.oncanplaythrough = () => {
|
| 159 |
+
isLoadingMusic = false; // Update the loading flag when ready to play
|
| 160 |
+
loaderContainer.innerHTML = ''; // Remove the loader element
|
| 161 |
+
player.style.display = ''; // Show the player UI
|
| 162 |
+
player.play(); // Start playback
|
| 163 |
+
player.loop = isLooping; // Ensure looping state is applied
|
| 164 |
};
|
| 165 |
|
| 166 |
player.onended = () => {
|
|
|
|
| 168 |
const currentIndex = trackList.indexOf(currentPlaying);
|
| 169 |
if (currentIndex !== -1 && currentIndex < trackList.length - 1) {
|
| 170 |
playMusic(trackList[currentIndex + 1]);
|
|
|
|
| 171 |
}
|
| 172 |
}
|
| 173 |
};
|
| 174 |
|
| 175 |
player.onerror = () => {
|
| 176 |
+
isLoadingMusic = false; // Ensure flag is reset on error
|
| 177 |
+
loaderContainer.innerHTML = ''; // Clear out the loader element
|
| 178 |
alert(`Error loading the music: ${fileName}`);
|
| 179 |
+
currentPlaying = ''; // Reset the current playing track
|
| 180 |
};
|
| 181 |
}
|