riddhiman commited on
Commit
03d04a3
·
verified ·
1 Parent(s): 922515a

Update public/script.js

Browse files
Files changed (1) hide show
  1. 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
- .then(response => response.json())
12
- .then(files => {
13
- const musicList = document.getElementById('music-list');
14
- musicList.innerHTML = ''; // Clear existing music items
15
- files.forEach(file => {
16
- if (file.toLowerCase().includes(searchQuery)) {
17
- // For simplicity, initially just indicate tracks are available but not show them directly
18
- const placeholder = document.createElement('div');
19
- placeholder.textContent = "Track available - press Play";
20
- const playButton = document.createElement('button');
21
- playButton.textContent = "Play";
22
- playButton.onclick = () => {
23
- musicList.innerHTML = ''; // Clear the placeholder
24
- const loader = document.createElement('div');
25
- loader.className = 'loader';
26
- musicList.appendChild(loader); // Add loader while waiting for music to load
27
- playMusic(file);
28
- };
29
- musicList.appendChild(placeholder);
30
- musicList.appendChild(playButton);
31
- }
32
- });
33
  });
 
34
  }
35
 
36
  function playMusic(fileName) {
37
  const player = document.getElementById('audio-player');
38
- player.src = `/music/${fileName}`;
39
- player.load(); // Force reload of the new source
40
-
41
- player.oncanplaythrough = () => {
42
- // Remove loader if there is any
43
- const loader = document.querySelector('.loader');
44
- if (loader) {
45
- loader.remove();
46
- }
47
 
48
- // Now that the track is loaded, adjust UI as needed e.g., show track info
49
- const trackInfo = document.createElement('div');
50
- trackInfo.textContent = `Now playing: ${fileName}`;
51
- document.getElementById('music-list').appendChild(trackInfo);
52
 
 
 
 
53
  player.play();
54
  };
55
 
56
  player.onerror = () => {
57
- // Handle loading error
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
  }