Spaces:
Runtime error
Runtime error
Update public/script.js
Browse files- public/script.js +39 -41
public/script.js
CHANGED
|
@@ -1,64 +1,62 @@
|
|
| 1 |
window.onload = () => {
|
| 2 |
loadMusicList();
|
| 3 |
-
// Event listener for search functionality
|
| 4 |
document.getElementById('search-bar').addEventListener('keyup', loadMusicList);
|
|
|
|
|
|
|
|
|
|
| 5 |
};
|
| 6 |
|
| 7 |
function loadMusicList() {
|
| 8 |
const searchQuery = document.getElementById('search-bar').value.toLowerCase();
|
| 9 |
|
| 10 |
fetch('/tracks')
|
| 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 |
function playMusic(fileName) {
|
| 37 |
const player = document.getElementById('audio-player');
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
loader.remove();
|
| 46 |
-
}
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
document.getElementById('music-list').appendChild(trackInfo);
|
| 52 |
|
|
|
|
|
|
|
|
|
|
| 53 |
player.play();
|
| 54 |
};
|
| 55 |
|
| 56 |
player.onerror = () => {
|
| 57 |
-
//
|
| 58 |
-
const loader = document.querySelector('.loader');
|
| 59 |
-
if (loader) {
|
| 60 |
-
loader.remove();
|
| 61 |
-
}
|
| 62 |
alert(`Error loading the music: ${fileName}`);
|
| 63 |
};
|
| 64 |
}
|
|
|
|
| 1 |
window.onload = () => {
|
| 2 |
loadMusicList();
|
|
|
|
| 3 |
document.getElementById('search-bar').addEventListener('keyup', loadMusicList);
|
| 4 |
+
|
| 5 |
+
// Initially hide the audio player until a track is ready to be played
|
| 6 |
+
document.getElementById('audio-player').style.display = 'none';
|
| 7 |
};
|
| 8 |
|
| 9 |
function loadMusicList() {
|
| 10 |
const searchQuery = document.getElementById('search-bar').value.toLowerCase();
|
| 11 |
|
| 12 |
fetch('/tracks')
|
| 13 |
+
.then(response => response.json())
|
| 14 |
+
.then(files => {
|
| 15 |
+
const musicList = document.getElementById('music-list');
|
| 16 |
+
musicList.innerHTML = ''; // Clear the list
|
| 17 |
+
files.forEach(file => {
|
| 18 |
+
if (file.toLowerCase().includes(searchQuery)) {
|
| 19 |
+
const musicItem = document.createElement('div');
|
| 20 |
+
musicItem.classList.add('music-item');
|
| 21 |
+
|
| 22 |
+
const playButton = document.createElement('button');
|
| 23 |
+
playButton.classList.add('play-button');
|
| 24 |
+
playButton.textContent = 'Play';
|
| 25 |
+
playButton.onclick = () => playMusic(file);
|
| 26 |
+
|
| 27 |
+
const fileNameSpan = document.createElement('span');
|
| 28 |
+
fileNameSpan.textContent = file;
|
| 29 |
+
|
| 30 |
+
musicItem.appendChild(playButton);
|
| 31 |
+
musicItem.appendChild(fileNameSpan);
|
| 32 |
+
musicList.appendChild(musicItem);
|
| 33 |
+
}
|
|
|
|
| 34 |
});
|
| 35 |
+
});
|
| 36 |
}
|
| 37 |
|
| 38 |
function playMusic(fileName) {
|
| 39 |
const player = document.getElementById('audio-player');
|
| 40 |
+
|
| 41 |
+
// Display the loader to indicate loading is in progress
|
| 42 |
+
const loaderElement = document.createElement('div');
|
| 43 |
+
loaderElement.textContent = 'Loading...'; // Text for simplicity, you could use a CSS-based loader as well
|
| 44 |
+
loaderElement.classList.add('loader');
|
| 45 |
+
const musicList = document.getElementById('music-list');
|
| 46 |
+
musicList.appendChild(loaderElement);
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
player.style.display = 'none'; // Hide player until the track is loaded
|
| 49 |
+
player.src = `/music/${fileName}`;
|
| 50 |
+
player.load();
|
|
|
|
| 51 |
|
| 52 |
+
player.oncanplaythrough = () => { // The track is fully loaded and ready to play
|
| 53 |
+
loaderElement.remove(); // Remove loader
|
| 54 |
+
player.style.display = ''; // Show the audio player
|
| 55 |
player.play();
|
| 56 |
};
|
| 57 |
|
| 58 |
player.onerror = () => {
|
| 59 |
+
loaderElement.remove(); // Remove loader if there's an error loading the track
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
alert(`Error loading the music: ${fileName}`);
|
| 61 |
};
|
| 62 |
}
|