|
| 1 | +const musicContainer = document.getElementById("music-container"); |
| 2 | +const playButton = document.getElementById("play"); |
| 3 | +const prevButton = document.getElementById("prev"); |
| 4 | +const nextButton = document.getElementById("next"); |
| 5 | +const audio = document.getElementById("audio"); |
| 6 | +const progress = document.getElementById("progress"); |
| 7 | +const progressContainer = document.getElementById("progress-container"); |
| 8 | +const title = document.getElementById("title"); |
| 9 | +const cover = document.getElementById("cover"); |
| 10 | + |
| 11 | +const songs = ["hey", "summer", "ukulele"]; |
| 12 | +let songIndex = 2; |
| 13 | + |
| 14 | +function getSongTitle(song) { |
| 15 | + return song.charAt(0).toUpperCase() + song.slice(1); |
| 16 | +} |
| 17 | + |
| 18 | +function loadSong(song) { |
| 19 | + title.innerText = getSongTitle(song); |
| 20 | + audio.src = `https://github.com/bradtraversy/vanillawebprojects/blob/master/music-player/music/${song}.mp3?raw=true`; |
| 21 | + cover.src = `https://github.com/bradtraversy/vanillawebprojects/blob/master/music-player/images/${song}.jpg?raw=true`; |
| 22 | +} |
| 23 | + |
| 24 | +function playSong() { |
| 25 | + musicContainer.classList.add("play"); |
| 26 | + playButton.querySelector("i.fas").classList.remove("fa-play"); |
| 27 | + playButton.querySelector("i.fas").classList.add("fa-pause"); |
| 28 | + audio.play(); |
| 29 | +} |
| 30 | + |
| 31 | +function pauseSong() { |
| 32 | + musicContainer.classList.remove("play"); |
| 33 | + playButton.querySelector("i.fas").classList.remove("fa-pause"); |
| 34 | + playButton.querySelector("i.fas").classList.add("fa-play"); |
| 35 | + audio.pause(); |
| 36 | +} |
| 37 | + |
| 38 | +function prevSong() { |
| 39 | + songIndex--; |
| 40 | + if (songIndex < 0) songIndex = songs.length - 1; |
| 41 | + loadSong(songs[songIndex]); |
| 42 | + playSong(); |
| 43 | +} |
| 44 | + |
| 45 | +function nextSong() { |
| 46 | + songIndex++; |
| 47 | + if (songIndex > songs.length - 1) songIndex = 0; |
| 48 | + loadSong(songs[songIndex]); |
| 49 | + playSong(); |
| 50 | +} |
| 51 | + |
| 52 | +function updateProgress(e) { |
| 53 | + const { duration, currentTime } = e.srcElement; |
| 54 | + const progressPercent = (currentTime / duration) * 100; |
| 55 | + progress.style.width = `${progressPercent}%`; |
| 56 | +} |
| 57 | + |
| 58 | +function setProgress(e) { |
| 59 | + const width = this.clientWidth; |
| 60 | + const clickX = e.offsetX; |
| 61 | + const duration = audio.duration; |
| 62 | + audio.currentTime = (clickX / width) * duration; |
| 63 | +} |
| 64 | + |
| 65 | +// Event Listeners |
| 66 | +playButton.addEventListener("click", () => { |
| 67 | + const isPlaying = musicContainer.classList.contains("play"); |
| 68 | + isPlaying ? pauseSong() : playSong(); |
| 69 | +}); |
| 70 | + |
| 71 | +prevButton.addEventListener("click", prevSong); |
| 72 | +nextButton.addEventListener("click", nextSong); |
| 73 | + |
| 74 | +audio.addEventListener("timeupdate", updateProgress); |
| 75 | +progressContainer.addEventListener("click", setProgress); |
| 76 | + |
| 77 | +audio.addEventListener("ended", nextSong); |
| 78 | + |
| 79 | +// Init |
| 80 | +loadSong(songs[songIndex]); |
0 commit comments