Spaces:
Runtime error
Runtime error
Update public/script.js
Browse files- public/script.js +47 -32
public/script.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
window.onload = () => {
|
| 2 |
loadMusicList();
|
| 3 |
document.getElementById('search-bar').addEventListener('keyup', loadMusicList);
|
| 4 |
-
|
| 5 |
-
document.getElementById('audio-player').style.display = 'none';
|
| 6 |
};
|
| 7 |
|
| 8 |
-
let isLoadingMusic = false;
|
| 9 |
-
let currentPlaying = '';
|
|
|
|
|
|
|
| 10 |
|
| 11 |
function loadMusicList() {
|
| 12 |
const searchQuery = document.getElementById('search-bar').value.toLowerCase();
|
|
@@ -14,10 +14,14 @@ function loadMusicList() {
|
|
| 14 |
fetch('/tracks')
|
| 15 |
.then(response => response.json())
|
| 16 |
.then(tracks => {
|
|
|
|
| 17 |
const musicList = document.getElementById('music-list');
|
| 18 |
musicList.innerHTML = '';
|
| 19 |
-
|
|
|
|
| 20 |
if (track.filename.toLowerCase().includes(searchQuery)) {
|
|
|
|
|
|
|
| 21 |
const musicItem = document.createElement('div');
|
| 22 |
musicItem.classList.add('music-item');
|
| 23 |
|
|
@@ -27,14 +31,9 @@ function loadMusicList() {
|
|
| 27 |
playButton.addEventListener('click', () => playMusic(track.filename));
|
| 28 |
|
| 29 |
const artworkImg = document.createElement('img');
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
artworkImg.classList.add('album-art');
|
| 34 |
-
} else {
|
| 35 |
-
artworkImg.alt = '';
|
| 36 |
-
artworkImg.classList.add('album-art');
|
| 37 |
-
}
|
| 38 |
|
| 39 |
const fileNameSpan = document.createElement('span');
|
| 40 |
fileNameSpan.textContent = track.filename;
|
|
@@ -49,47 +48,63 @@ function loadMusicList() {
|
|
| 49 |
}
|
| 50 |
|
| 51 |
function playMusic(fileName) {
|
| 52 |
-
|
|
|
|
| 53 |
if (isLoadingMusic || fileName === currentPlaying) {
|
| 54 |
-
console.log("This song is already loading or playing.");
|
| 55 |
return;
|
| 56 |
}
|
| 57 |
|
| 58 |
-
isLoadingMusic = true;
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
const player = document.getElementById('audio-player');
|
| 61 |
-
|
| 62 |
-
// Remove any existing loader to ensure we don't append multiple loaders
|
| 63 |
const existingLoader = document.querySelector('.loader-container');
|
| 64 |
-
if (existingLoader)
|
| 65 |
-
existingLoader.remove();
|
| 66 |
-
}
|
| 67 |
|
| 68 |
-
//
|
| 69 |
const loaderContainer = document.createElement('div');
|
| 70 |
loaderContainer.classList.add('loader-container');
|
| 71 |
const loaderElement = document.createElement('div');
|
| 72 |
loaderElement.classList.add('loader');
|
| 73 |
loaderContainer.appendChild(loaderElement);
|
| 74 |
-
const musicList = document.getElementById('music-list');
|
| 75 |
-
musicList.appendChild(loaderContainer);
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
player.src = `/music/${fileName}`;
|
| 79 |
player.load();
|
| 80 |
|
| 81 |
player.oncanplaythrough = () => {
|
| 82 |
-
isLoadingMusic = false;
|
| 83 |
loaderContainer.remove();
|
| 84 |
player.style.display = '';
|
| 85 |
player.play();
|
| 86 |
-
currentPlaying = fileName; // Update currentPlaying with the filename of the track that just loaded
|
| 87 |
};
|
| 88 |
|
|
|
|
|
|
|
| 89 |
player.onerror = () => {
|
| 90 |
-
isLoadingMusic = false; // Reset the flag in case of an error
|
| 91 |
-
loaderContainer.remove();
|
| 92 |
alert(`Error loading the music: ${fileName}`);
|
| 93 |
-
|
|
|
|
| 94 |
};
|
| 95 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
window.onload = () => {
|
| 2 |
loadMusicList();
|
| 3 |
document.getElementById('search-bar').addEventListener('keyup', loadMusicList);
|
|
|
|
|
|
|
| 4 |
};
|
| 5 |
|
| 6 |
+
let isLoadingMusic = false;
|
| 7 |
+
let currentPlaying = '';
|
| 8 |
+
let isLooping = false;
|
| 9 |
+
let trackList = [];
|
| 10 |
|
| 11 |
function loadMusicList() {
|
| 12 |
const searchQuery = document.getElementById('search-bar').value.toLowerCase();
|
|
|
|
| 14 |
fetch('/tracks')
|
| 15 |
.then(response => response.json())
|
| 16 |
.then(tracks => {
|
| 17 |
+
trackList = [];
|
| 18 |
const musicList = document.getElementById('music-list');
|
| 19 |
musicList.innerHTML = '';
|
| 20 |
+
|
| 21 |
+
tracks.forEach((track, index) => {
|
| 22 |
if (track.filename.toLowerCase().includes(searchQuery)) {
|
| 23 |
+
trackList.push(track.filename);
|
| 24 |
+
|
| 25 |
const musicItem = document.createElement('div');
|
| 26 |
musicItem.classList.add('music-item');
|
| 27 |
|
|
|
|
| 31 |
playButton.addEventListener('click', () => playMusic(track.filename));
|
| 32 |
|
| 33 |
const artworkImg = document.createElement('img');
|
| 34 |
+
artworkImg.src = track.artwork || "";
|
| 35 |
+
artworkImg.alt = track.artwork ? 'Album Art' : '';
|
| 36 |
+
artworkImg.classList.add('album-art');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
const fileNameSpan = document.createElement('span');
|
| 39 |
fileNameSpan.textContent = track.filename;
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
function playMusic(fileName) {
|
| 51 |
+
const player = document.getElementById('audio-player');
|
| 52 |
+
|
| 53 |
if (isLoadingMusic || fileName === currentPlaying) {
|
| 54 |
+
console.log("This song is already loading or playing.");
|
| 55 |
return;
|
| 56 |
}
|
| 57 |
|
| 58 |
+
isLoadingMusic = true;
|
| 59 |
+
currentPlaying = fileName;
|
| 60 |
+
|
| 61 |
+
player.style.display = 'none';
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
const existingLoader = document.querySelector('.loader-container');
|
| 64 |
+
if (existingLoader) existingLoader.remove();
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
// Loader setup
|
| 67 |
const loaderContainer = document.createElement('div');
|
| 68 |
loaderContainer.classList.add('loader-container');
|
| 69 |
const loaderElement = document.createElement('div');
|
| 70 |
loaderElement.classList.add('loader');
|
| 71 |
loaderContainer.appendChild(loaderElement);
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
document.getElementById('music-list').appendChild(loaderContainer);
|
| 74 |
+
|
| 75 |
player.src = `/music/${fileName}`;
|
| 76 |
player.load();
|
| 77 |
|
| 78 |
player.oncanplaythrough = () => {
|
| 79 |
+
isLoadingMusic = false;
|
| 80 |
loaderContainer.remove();
|
| 81 |
player.style.display = '';
|
| 82 |
player.play();
|
|
|
|
| 83 |
};
|
| 84 |
|
| 85 |
+
player.onended = () => handleTrackEnd();
|
| 86 |
+
|
| 87 |
player.onerror = () => {
|
|
|
|
|
|
|
| 88 |
alert(`Error loading the music: ${fileName}`);
|
| 89 |
+
isLoadingMusic = false;
|
| 90 |
+
loaderContainer.remove();
|
| 91 |
};
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
function handleTrackEnd() {
|
| 95 |
+
if (!isLooping) {
|
| 96 |
+
const currentIndex = trackList.indexOf(currentPlaying);
|
| 97 |
+
if (currentIndex !== -1 && currentIndex < trackList.length - 1) {
|
| 98 |
+
playMusic(trackList[currentIndex + 1]);
|
| 99 |
+
} else {
|
| 100 |
+
currentPlaying = ''; // Optionally reset playing track at the end of the list
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
function toggleLoop() {
|
| 106 |
+
isLooping = !isLooping;
|
| 107 |
+
const loopButton = document.getElementById('loop-toggle');
|
| 108 |
+
loopButton.textContent = `Loop: ${isLooping ? 'On' : 'Off'}`;
|
| 109 |
+
document.getElementById('audio-player').loop = isLooping;
|
| 110 |
+
}}
|