You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 lines
1.5 KiB

  1. const playerButton = document.querySelector('.player-button'),
  2. audio = document.querySelector('audio'),
  3. playIcon = `
  4. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="rgb(255 255 255/var(--tw-bg-opacity))">
  5. <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM9.555 7.168A1 1 0 008 8v4a1 1 0 001.555.832l3-2a1 1 0 000-1.664l-3-2z" clip-rule="evenodd" />
  6. </svg>
  7. `,
  8. pauseIcon = `
  9. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="rgb(255 255 255/var(--tw-bg-opacity))">
  10. <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zM7 8a1 1 0 012 0v4a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v4a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd" />
  11. </svg>
  12. `;
  13. function toggleAudio() {
  14. if (audio.paused) {
  15. audio.play();
  16. playerButton.innerHTML = pauseIcon;
  17. } else {
  18. audio.pause();
  19. playerButton.innerHTML = playIcon;
  20. }
  21. }
  22. playerButton.addEventListener('click', toggleAudio);
  23. function audioEnded() {
  24. playerButton.innerHTML = playIcon;
  25. }
  26. audio.onended = audioEnded;
  27. const timeline = document.querySelector('.timeline');
  28. function changeTimelinePosition() {
  29. const percentagePosition = (100 * audio.currentTime) / audio.duration;
  30. timeline.style.backgroundSize = `${percentagePosition}% 100%`;
  31. timeline.value = percentagePosition;
  32. }
  33. audio.ontimeupdate = changeTimelinePosition;
  34. function changeSeek() {
  35. const time = (timeline.value * audio.duration) / 100;
  36. audio.currentTime = time;
  37. }
  38. timeline.addEventListener('change', changeSeek);