|
| 1 | +let audioPlayer = document.getElementById("audio-player") |
| 2 | +let progressBar = document.getElementById("progressBar") |
| 3 | +let timer = document.querySelector(".audio-player-wrapper__timer") |
| 4 | +let audioPlayerWrapper = document.querySelector(".audio-player-wrapper") |
| 5 | + |
| 6 | +// controls |
| 7 | +let playBtn = document.querySelector(".audio-player-wrapper__play") |
| 8 | +// let pauseBtn = document.querySelector(".audio-player-wrapper__pause") |
| 9 | +let skipPrev = document.querySelector(".audio-player-wrapper__skipPrev") |
| 10 | +let skipNext = document.querySelector(".audio-player-wrapper__nextSkip") |
| 11 | + |
| 12 | +document.addEventListener("DOMContentLoaded", () => { |
| 13 | + audioPlayer.addEventListener("timeupdate", () => { |
| 14 | + const progress = (audioPlayer.currentTime / audioPlayer.duration) * 100 |
| 15 | + progressBar.value = progress |
| 16 | + |
| 17 | + // calculating the the total time |
| 18 | + let totalSeconds = Math.floor(audioPlayer.duration) |
| 19 | + let minutes = Math.floor(totalSeconds / 60) |
| 20 | + let seconds = totalSeconds % 60 |
| 21 | + |
| 22 | + // Calculating the minutes, seconds based on real time update |
| 23 | + let changeTotalSeconds = Math.floor(audioPlayer.currentTime) |
| 24 | + let changeMinutes = Math.floor(changeTotalSeconds / 60) |
| 25 | + let changeSeconds = changeTotalSeconds % 60 |
| 26 | + |
| 27 | + timer.innerHTML = ` ${changeMinutes |
| 28 | + .toString() |
| 29 | + .padStart(2, "0")}:${changeSeconds |
| 30 | + .toString() |
| 31 | + .padStart(2, "0")} / ${minutes}: ${seconds.toString().padStart(2, "0")}` |
| 32 | + }) |
| 33 | + |
| 34 | + audioPlayer.addEventListener("play", () => { |
| 35 | + // changing the icons based on play and pause behavior of the audio |
| 36 | + playBtn.innerHTML = ` |
| 37 | + <svg |
| 38 | + xmlns="http://www.w3.org/2000/svg" |
| 39 | + width="16" |
| 40 | + height="16" |
| 41 | + fill="currentColor" |
| 42 | + class="bi bi-pause" |
| 43 | + viewBox="0 0 16 16" |
| 44 | + > |
| 45 | + <path d="M6 3.5a.5.5 0 0 1 .5.5v8a.5.5 0 0 1-1 0V4a.5.5 0 0 1 .5-.5m4 0a.5.5 0 0 1 .5.5v8a.5.5 0 0 1-1 0V4a.5.5 0 0 1 .5-.5" /> |
| 46 | + </svg> |
| 47 | + ` |
| 48 | + |
| 49 | + // if audio is playing and we click on playBtn, this event is call. |
| 50 | + playBtn.addEventListener("click", () => { |
| 51 | + audioPlayer.pause() |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + audioPlayer.addEventListener("pause", () => { |
| 56 | + // changing the icon based on play and pause functionality |
| 57 | + playBtn.innerHTML = ` |
| 58 | + <svg |
| 59 | + xmlns="http://www.w3.org/2000/svg" |
| 60 | + width="16" |
| 61 | + height="16" |
| 62 | + fill="currentColor" |
| 63 | + class="bi bi-play-fill" |
| 64 | + viewBox="0 0 16 16" |
| 65 | + > |
| 66 | + <path d="m11.596 8.697-6.363 3.692c-.54.313-1.233-.066-1.233-.697V4.308c0-.63.692-1.01 1.233-.696l6.363 3.692a.802.802 0 0 1 0 1.393" /> |
| 67 | + </svg> |
| 68 | + ` |
| 69 | + |
| 70 | + // this call if audio is pause |
| 71 | + playBtn.addEventListener("click", () => { |
| 72 | + audioPlayer.play() |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + // by default it is call when we click on playBtn |
| 77 | + playBtn.addEventListener("click", () => { |
| 78 | + audioPlayer.play() |
| 79 | + }) |
| 80 | + |
| 81 | + skipPrev.addEventListener("click", () => { |
| 82 | + audioPlayer.currentTime = Math.max(0, audioPlayer.currentTime - 10) |
| 83 | + }) |
| 84 | + |
| 85 | + skipNext.addEventListener("click", () => { |
| 86 | + audioPlayer.currentTime = Math.max(0, audioPlayer.currentTime + 10) |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +// =================================== start of the styles |
| 91 | +// audioPlayerWrapper.innerHTML = "" |
| 92 | +audioPlayerWrapper.style.background = "#FFF5E4" |
| 93 | + |
| 94 | +// ===================================== end of the styles |
0 commit comments