Spaces:
Runtime error
Runtime error
Create script.js
Browse files
script.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
let audio = new Audio();
|
| 2 |
+
let currentPlaying = '';
|
| 3 |
+
|
| 4 |
+
document.getElementById('toggle-play').addEventListener('click', function() {
|
| 5 |
+
if (audio.paused) {
|
| 6 |
+
audio.play();
|
| 7 |
+
this.textContent = 'Pause';
|
| 8 |
+
} else {
|
| 9 |
+
audio.pause();
|
| 10 |
+
this.textContent = 'Play';
|
| 11 |
+
}
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
function playMusic(filename) {
|
| 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 filteredFiles = files.filter(file => file.includes(query));
|
| 29 |
+
const musicListEl = document.getElementById('music-list');
|
| 30 |
+
musicListEl.innerHTML = '';
|
| 31 |
+
filteredFiles.forEach(file => {
|
| 32 |
+
const div = document.createElement('div');
|
| 33 |
+
div.textContent = file;
|
| 34 |
+
div.onclick = () => playMusic(file);
|
| 35 |
+
musicListEl.appendChild(div);
|
| 36 |
+
});
|
| 37 |
+
document.getElementById('loading').style.display = 'none';
|
| 38 |
+
});
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// Search feature
|
| 42 |
+
document.getElementById('search').addEventListener('input', function() {
|
| 43 |
+
fetchMusicList(this.value);
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
// Initial fetch
|
| 47 |
+
fetchMusicList();
|