Spaces:
Runtime error
Runtime error
Update public/script.js
Browse files- public/script.js +31 -5
public/script.js
CHANGED
|
@@ -13,10 +13,20 @@ function loadMusicList() {
|
|
| 13 |
musicList.innerHTML = '';
|
| 14 |
files.forEach(file => {
|
| 15 |
if (file.toLowerCase().includes(searchQuery)) {
|
| 16 |
-
const
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
});
|
| 22 |
});
|
|
@@ -24,6 +34,22 @@ function loadMusicList() {
|
|
| 24 |
|
| 25 |
function playMusic(fileName) {
|
| 26 |
const player = document.getElementById('audio-player');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
player.src = `/music/${fileName}`;
|
| 28 |
-
player.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
|
|
|
| 13 |
musicList.innerHTML = '';
|
| 14 |
files.forEach(file => {
|
| 15 |
if (file.toLowerCase().includes(searchQuery)) {
|
| 16 |
+
const musicItem = document.createElement('div');
|
| 17 |
+
musicItem.classList.add('music-item');
|
| 18 |
+
|
| 19 |
+
const playButton = document.createElement('button');
|
| 20 |
+
playButton.classList.add('play-button');
|
| 21 |
+
playButton.innerHTML = 'Play';
|
| 22 |
+
playButton.addEventListener('click', () => playMusic(file));
|
| 23 |
+
|
| 24 |
+
const fileNameSpan = document.createElement('span');
|
| 25 |
+
fileNameSpan.textContent = file;
|
| 26 |
+
|
| 27 |
+
musicItem.appendChild(playButton);
|
| 28 |
+
musicItem.appendChild(fileNameSpan);
|
| 29 |
+
musicList.appendChild(musicItem);
|
| 30 |
}
|
| 31 |
});
|
| 32 |
});
|
|
|
|
| 34 |
|
| 35 |
function playMusic(fileName) {
|
| 36 |
const player = document.getElementById('audio-player');
|
| 37 |
+
const loader = document.createElement('div');
|
| 38 |
+
loader.classList.add('loader');
|
| 39 |
+
|
| 40 |
+
const musicList = document.getElementById('music-list');
|
| 41 |
+
musicList.appendChild(loader); // Show loader
|
| 42 |
+
|
| 43 |
player.src = `/music/${fileName}`;
|
| 44 |
+
player.load(); // Important to reload if changing source
|
| 45 |
+
|
| 46 |
+
player.oncanplaythrough = () => { // Use the 'canplaythrough' event to know when the track is loaded
|
| 47 |
+
loader.remove(); // Hide loader when music is ready to play
|
| 48 |
+
player.play();
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
player.onerror = () => { // In case there is an error loading the music file.
|
| 52 |
+
loader.remove();
|
| 53 |
+
alert(`Error loading the music: ${fileName}`);
|
| 54 |
+
};
|
| 55 |
}
|