AdityaAdaki commited on
Commit
eafa2f5
·
1 Parent(s): fe7af1f

css update

Browse files
Files changed (1) hide show
  1. static/js/main.js +52 -9
static/js/main.js CHANGED
@@ -325,7 +325,10 @@ function setupPlayerControls() {
325
  prevBtn.addEventListener('click', playPrevious);
326
  nextBtn.addEventListener('click', playNext);
327
  volumeSlider.addEventListener('input', updateVolume);
 
 
328
  seekSlider.addEventListener('input', seekTo);
 
329
 
330
  // Add keyboard controls
331
  document.addEventListener('keydown', (e) => {
@@ -339,10 +342,20 @@ function setupPlayerControls() {
339
  }
340
  });
341
 
342
- // Update time display
343
  if (audioElement) {
 
 
 
 
 
 
344
  audioElement.addEventListener('timeupdate', updateTimeDisplay);
 
345
  audioElement.addEventListener('ended', handleTrackEnd);
 
 
 
346
  }
347
  }
348
 
@@ -389,8 +402,15 @@ function updateVolume(e) {
389
  // Seek to position
390
  function seekTo(e) {
391
  if (audioElement && audioElement.duration) {
392
- const time = (e.target.value / 100) * audioElement.duration;
 
 
 
393
  audioElement.currentTime = time;
 
 
 
 
394
  }
395
  }
396
 
@@ -403,14 +423,24 @@ function updateTimeDisplay() {
403
  const seekSlider = document.querySelector('.seek-slider');
404
  const progress = document.querySelector('.progress');
405
 
406
- const current = formatTime(audioElement.currentTime);
407
- const total = formatTime(audioElement.duration);
408
- const progressPercent = (audioElement.currentTime / audioElement.duration) * 100;
 
 
409
 
410
- currentTime.textContent = current;
411
- totalTime.textContent = total;
412
- seekSlider.value = progressPercent;
413
- progress.style.width = `${progressPercent}%`;
 
 
 
 
 
 
 
 
414
  }
415
 
416
  // Format time in MM:SS
@@ -559,8 +589,20 @@ function playTrack(index) {
559
  if (prevBtn) prevBtn.disabled = false;
560
  if (nextBtn) nextBtn.disabled = false;
561
 
 
 
 
 
 
 
 
 
562
  // Update audio source and play
563
  audioElement.src = track.url;
 
 
 
 
564
  audioElement.play()
565
  .then(() => {
566
  isPlaying = true;
@@ -568,6 +610,7 @@ function playTrack(index) {
568
  playPauseBtn.innerHTML = '<i class="fas fa-pause"></i>';
569
  }
570
  updateNowPlayingInfo();
 
571
  })
572
  .catch(error => {
573
  console.error('Error playing track:', error);
 
325
  prevBtn.addEventListener('click', playPrevious);
326
  nextBtn.addEventListener('click', playNext);
327
  volumeSlider.addEventListener('input', updateVolume);
328
+
329
+ // Add both input and change events for the seek slider
330
  seekSlider.addEventListener('input', seekTo);
331
+ seekSlider.addEventListener('change', seekTo);
332
 
333
  // Add keyboard controls
334
  document.addEventListener('keydown', (e) => {
 
342
  }
343
  });
344
 
345
+ // Update time display more frequently
346
  if (audioElement) {
347
+ // Remove existing listeners first
348
+ audioElement.removeEventListener('timeupdate', updateTimeDisplay);
349
+ audioElement.removeEventListener('loadedmetadata', updateTimeDisplay);
350
+ audioElement.removeEventListener('ended', handleTrackEnd);
351
+
352
+ // Add listeners
353
  audioElement.addEventListener('timeupdate', updateTimeDisplay);
354
+ audioElement.addEventListener('loadedmetadata', updateTimeDisplay);
355
  audioElement.addEventListener('ended', handleTrackEnd);
356
+
357
+ // Force initial update
358
+ updateTimeDisplay();
359
  }
360
  }
361
 
 
402
  // Seek to position
403
  function seekTo(e) {
404
  if (audioElement && audioElement.duration) {
405
+ const seekSlider = document.querySelector('.seek-slider');
406
+ const progress = document.querySelector('.progress');
407
+ const time = (seekSlider.value / 100) * audioElement.duration;
408
+
409
  audioElement.currentTime = time;
410
+ progress.style.width = `${seekSlider.value}%`;
411
+
412
+ // Force time display update
413
+ updateTimeDisplay();
414
  }
415
  }
416
 
 
423
  const seekSlider = document.querySelector('.seek-slider');
424
  const progress = document.querySelector('.progress');
425
 
426
+ // Only update if duration is available and not NaN
427
+ if (audioElement.duration && !isNaN(audioElement.duration)) {
428
+ const current = formatTime(audioElement.currentTime);
429
+ const total = formatTime(audioElement.duration);
430
+ const progressPercent = (audioElement.currentTime / audioElement.duration) * 100;
431
 
432
+ // Update current time display
433
+ if (currentTime) currentTime.textContent = current;
434
+
435
+ // Update total time display
436
+ if (totalTime) totalTime.textContent = total;
437
+
438
+ // Update slider and progress bar
439
+ if (seekSlider && !seekSlider.matches(':active')) { // Only update if user isn't dragging
440
+ seekSlider.value = progressPercent;
441
+ progress.style.width = `${progressPercent}%`;
442
+ }
443
+ }
444
  }
445
 
446
  // Format time in MM:SS
 
589
  if (prevBtn) prevBtn.disabled = false;
590
  if (nextBtn) nextBtn.disabled = false;
591
 
592
+ // Remove any existing event listeners before adding new ones
593
+ audioElement.removeEventListener('loadedmetadata', updateTimeDisplay);
594
+ audioElement.removeEventListener('timeupdate', updateTimeDisplay);
595
+
596
+ // Add event listeners
597
+ audioElement.addEventListener('loadedmetadata', updateTimeDisplay);
598
+ audioElement.addEventListener('timeupdate', updateTimeDisplay);
599
+
600
  // Update audio source and play
601
  audioElement.src = track.url;
602
+
603
+ // Force an initial time display update
604
+ setTimeout(updateTimeDisplay, 100);
605
+
606
  audioElement.play()
607
  .then(() => {
608
  isPlaying = true;
 
610
  playPauseBtn.innerHTML = '<i class="fas fa-pause"></i>';
611
  }
612
  updateNowPlayingInfo();
613
+ updateTimeDisplay(); // Update time display immediately after play starts
614
  })
615
  .catch(error => {
616
  console.error('Error playing track:', error);