Spaces:
Runtime error
Runtime error
Update public/script.js
Browse files- public/script.js +21 -39
public/script.js
CHANGED
|
@@ -1,47 +1,29 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
audio.play();
|
| 7 |
-
this.textContent = 'Pause';
|
| 8 |
-
} else {
|
| 9 |
-
audio.pause();
|
| 10 |
-
this.textContent = 'Play';
|
| 11 |
-
}
|
| 12 |
-
});
|
| 13 |
|
| 14 |
-
|
| 15 |
-
if (currentPlaying !== filename) {
|
| 16 |
-
audio.src = `/music/${filename}`;
|
| 17 |
-
currentPlaying = filename;
|
| 18 |
-
}
|
| 19 |
-
audio.play();
|
| 20 |
-
document.getElementById('toggle-play').textContent = 'Pause';
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
function fetchMusicList(query = '') {
|
| 24 |
-
document.getElementById('loading').style.display = 'block';
|
| 25 |
-
fetch('/music-list')
|
| 26 |
.then(response => response.json())
|
| 27 |
.then(files => {
|
| 28 |
-
const
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
});
|
| 37 |
-
document.getElementById('loading').style.display = 'none';
|
| 38 |
});
|
| 39 |
}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
document.getElementById('
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
// Initial fetch
|
| 47 |
-
fetchMusicList();
|
|
|
|
| 1 |
+
window.onload = () => {
|
| 2 |
+
loadMusicList();
|
| 3 |
+
document.getElementById('search-bar').addEventListener('keyup', loadMusicList);
|
| 4 |
+
};
|
| 5 |
|
| 6 |
+
function loadMusicList() {
|
| 7 |
+
const searchQuery = document.getElementById('search-bar').value.toLowerCase();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
fetch('/tracks')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
.then(response => response.json())
|
| 11 |
.then(files => {
|
| 12 |
+
const musicList = document.getElementById('music-list');
|
| 13 |
+
musicList.innerHTML = '';
|
| 14 |
+
files.forEach(file => {
|
| 15 |
+
if (file.toLowerCase().includes(searchQuery)) {
|
| 16 |
+
const listItem = document.createElement('div');
|
| 17 |
+
listItem.innerHTML = file;
|
| 18 |
+
listItem.addEventListener('click', () => playMusic(file));
|
| 19 |
+
musicList.appendChild(listItem);
|
| 20 |
+
}
|
| 21 |
});
|
|
|
|
| 22 |
});
|
| 23 |
}
|
| 24 |
|
| 25 |
+
function playMusic(fileName) {
|
| 26 |
+
const player = document.getElementById('audio-player');
|
| 27 |
+
player.src = `/music/${fileName}`;
|
| 28 |
+
player.play();
|
| 29 |
+
}
|
|
|
|
|
|