-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmusic-player.js
More file actions
158 lines (137 loc) · 4.21 KB
/
Copy pathmusic-player.js
File metadata and controls
158 lines (137 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const playlistSongs = document.getElementById("playlist-songs");
const playButton = document.getElementById("play");
const pauseButton = document.getElementById("pause");
const nextButton = document.getElementById("next");
const previousButton = document.getElementById("previous");
const playingSong = document.getElementById("player-song-title");
const songArtist = document.getElementById("player-song-artist");
const allSongs = [
{
id: 0,
title: "Hello World",
artist: "Rafael",
duration: "0:23",
src: "https://cdn.freecodecamp.org/curriculum/js-music-player/hello-world.mp3",
},
{
id: 1,
title: "In the Zone",
artist: "Rafael",
duration: "0:11",
src: "https://cdn.freecodecamp.org/curriculum/js-music-player/in-the-zone.mp3",
},
{
id: 2,
title: "Camper Cat",
artist: "Rafael",
duration: "0:21",
src: "https://cdn.freecodecamp.org/curriculum/js-music-player/camper-cat.mp3",
},
{
id: 3,
title: "Electronic",
artist: "Rafael",
duration: "0:15",
src: "https://cdn.freecodecamp.org/curriculum/js-music-player/electronic.mp3",
},
{
id: 4,
title: "Sailing Away",
artist: "Rafael",
duration: "0:22",
src: "https://cdn.freecodecamp.org/curriculum/js-music-player/sailing-away.mp3",
},
];
const audio = new Audio();
const userData = {
songs: allSongs,
currentSong: null,
songCurrentTime: 0,
};
const playSong = (id, start=true) => {
const song = userData.songs.find((song) => song.id === id);
audio.src = song.src;
audio.title = song.title;
if (userData.currentSong === null || start) {
audio.currentTime = 0;
} else {
audio.currentTime = userData.songCurrentTime;
}
userData.currentSong = song;
playButton.classList.add("playing");
setPlayerDisplay();
highlightCurrentSong();
setPlayButtonAccessibleText();
audio.play();
};
const pauseSong = () => {
userData.songCurrentTime = audio.currentTime;
playButton.classList.remove("playing");
audio.pause();
};
const getCurrentSongIndex = () => userData.songs.indexOf(userData.currentSong);
const getNextSong = () => userData.songs[getCurrentSongIndex() + 1];
const getPreviousSong = () => userData.songs[getCurrentSongIndex() - 1];
const playPreviousSong = () => {
if (userData.currentSong === null) return;
const previousSong = getPreviousSong();
if (previousSong) {
playSong(previousSong.id);
} else {
playSong(userData.songs[0].id);
}
};
const playNextSong = () => {
if (userData.currentSong === null) {
playSong(userData.songs[0].id);
return;
}
const nextSong = getNextSong();
if (nextSong) {
playSong(nextSong.id);
} else {
userData.currentSong = null;
userData.songCurrentTime = 0;
setPlayerDisplay();
highlightCurrentSong();
setPlayButtonAccessibleText();
pauseSong();
}
};
const setPlayerDisplay = () => {
const currentTitle = userData.currentSong?.title;
const currentArtist = userData.currentSong?.artist;
playingSong.textContent = currentTitle ? currentTitle : "";
songArtist.textContent = currentArtist ? currentArtist : "";
};
const highlightCurrentSong = () => {
const previousCurrentSong = document.querySelector('.playlist-song[aria-current="true"]');
previousCurrentSong?.removeAttribute("aria-current");
const songToHighlight = document.getElementById(
`song-${userData.currentSong?.id}`
);
songToHighlight?.setAttribute("aria-current", "true");
};
const setPlayButtonAccessibleText = () => {
const song = userData.currentSong;
playButton.setAttribute("aria-label", userData.currentSong ? `Play ${song.title}` : "Play");
};
playButton.addEventListener("click", () => {
if (userData.currentSong === null) {
playSong(userData.songs[0].id);
} else {
playSong(userData.currentSong.id, false);
}
});
const songs = document.querySelectorAll(".playlist-song");
songs.forEach((song) => {
const id = song.getAttribute("id").slice(5);
const songBtn = song.querySelector("button");
songBtn.addEventListener("click", () => {
playSong(Number(id));
});
});
pauseButton.addEventListener("click", pauseSong);
nextButton.addEventListener("click", playNextSong);
previousButton.addEventListener("click", playPreviousSong);
audio.addEventListener("ended", playNextSong);