riddhiman commited on
Commit
36864be
·
verified ·
1 Parent(s): 7e66b3c

Update public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +12 -14
public/script.js CHANGED
@@ -215,20 +215,24 @@ function playMusic(fileName) {
215
  }
216
 
217
  isLoadingMusic = true;
218
- showLoader(true); // Show loader when starting to load a new song
219
  currentPlaying = fileName;
220
 
221
  updatePlayingIndicator();
222
 
223
- document.getElementById('audio-player').style.display = 'flex'; // Show custom player UI
 
 
 
 
224
 
225
  // Set the new source file for audio
226
  audio.src = `/music/${fileName}`;
227
  audio.load(); // Start loading the new source file
228
 
229
  audio.oncanplaythrough = () => {
 
 
230
  isLoadingMusic = false;
231
- showLoader(false); // Hide loader when song is ready
232
  document.getElementById('play-pause-btn').textContent = 'Pause'; // Update play/pause button
233
  audio.play(); // Start playback
234
  };
@@ -236,26 +240,20 @@ function playMusic(fileName) {
236
  // Updated onended event handler within playMusic function
237
  audio.onended = () => {
238
  if (isLooping) {
239
- // Replay the current song
240
- audio.currentTime = 0;
241
- audio.play();
242
  } else {
243
  const currentIndex = trackList.indexOf(currentPlaying);
244
  if (currentIndex !== -1 && currentIndex < trackList.length - 1) {
245
- playMusic(trackList[currentIndex + 1]);
246
  }
247
  }
248
- };
249
 
250
  audio.onerror = () => {
251
  isLoadingMusic = false;
252
- showLoader(false); // Hide loader on error
253
  alert(`Error loading the music: ${fileName}`);
254
  currentPlaying = ''; // Reset the current playing track
255
  };
256
- }
257
-
258
- function showLoader(isVisible) {
259
- const loader = document.getElementById('loader');
260
- loader.style.display = isVisible ? 'block' : 'none'; // Toggle visibility based on `isVisible`
261
  }
 
215
  }
216
 
217
  isLoadingMusic = true;
 
218
  currentPlaying = fileName;
219
 
220
  updatePlayingIndicator();
221
 
222
+ const loaderContainer = document.getElementById('loader-container');
223
+ loaderContainer.innerHTML = '';
224
+ const loaderElement = document.createElement('div');
225
+ loaderElement.classList.add('loader');
226
+ loaderContainer.appendChild(loaderElement);
227
 
228
  // Set the new source file for audio
229
  audio.src = `/music/${fileName}`;
230
  audio.load(); // Start loading the new source file
231
 
232
  audio.oncanplaythrough = () => {
233
+ document.getElementById('audio-player').style.display = 'flex'; // Show custom player UI
234
+ loaderContainer.innerHTML = '';
235
  isLoadingMusic = false;
 
236
  document.getElementById('play-pause-btn').textContent = 'Pause'; // Update play/pause button
237
  audio.play(); // Start playback
238
  };
 
240
  // Updated onended event handler within playMusic function
241
  audio.onended = () => {
242
  if (isLooping) {
243
+ // Replay the current song
244
+ audio.currentTime = 0;
245
+ audio.play();
246
  } else {
247
  const currentIndex = trackList.indexOf(currentPlaying);
248
  if (currentIndex !== -1 && currentIndex < trackList.length - 1) {
249
+ playMusic(trackList[currentIndex + 1]);
250
  }
251
  }
252
+ };
253
 
254
  audio.onerror = () => {
255
  isLoadingMusic = false;
 
256
  alert(`Error loading the music: ${fileName}`);
257
  currentPlaying = ''; // Reset the current playing track
258
  };
 
 
 
 
 
259
  }