Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
164 lines (141 loc) · 5.53 KB
/
Copy pathscript.js
File metadata and controls
164 lines (141 loc) · 5.53 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
159
160
161
162
163
164
document.addEventListener('DOMContentLoaded', () => {
const audio = document.getElementById('chill-audio');
const masterPlay = document.getElementById('master-play');
const songTitle = document.getElementById('current-song-title');
const songArtist = document.getElementById('current-song-artist');
const songImg = document.getElementById('current-song-img');
const volumeSlider = document.getElementById('volume-slider');
let currentSongKey = null;
const songs = {
chill: {
title: "Lose My Mind",
artist: "Don Toliver ft. Doja Cat",
src: "Don_Toliver_Ft_Doja_Cat_-_Lose_My_Mind_Offblogmedia.com.mp3",
img: "https://www.shutterstock.com/shutterstock/photos/2194960155/display_1500/stock-photo-travel-winter-lake-baikal-happy-joy-woman-tourist-lie-on-ice-sunset-top-view-2194960155.jpg"
},
rock: {
title: "Do I Wanna Know?",
artist: "Arctic Monkeys",
src: "Do-I-Wanna-Know.mp3",
img: "https://www.shutterstock.com/shutterstock/photos/2028980156/display_1500/stock-photo-back-view-of-pre-teen-girl-enjoying-breathtaking-view-of-winter-landscape-in-oulanka-national-park-2028980156.jpg"
}
};
function updateAllCardIcons(key, isPlaying) {
const buttons = document.querySelectorAll(`.play-btn[data-song="${key}"]`);
buttons.forEach(btn => {
const icon = btn.querySelector('i');
if (isPlaying) {
icon.classList.remove('fa-play');
icon.classList.add('fa-pause');
} else {
icon.classList.remove('fa-pause');
icon.classList.add('fa-play');
}
});
}
// Reset ALL icons to play (when switching songs)
function resetAllIcons() {
const allIcons = document.querySelectorAll('.play-btn i');
allIcons.forEach(icon => {
icon.classList.remove('fa-pause');
icon.classList.add('fa-play');
});
}
function updateMasterPlay(isPlaying) {
if (isPlaying) {
masterPlay.classList.remove('fa-circle-play');
masterPlay.classList.remove('fa-solid');
masterPlay.classList.add('fa-regular');
masterPlay.classList.add('fa-circle-pause');
masterPlay.classList.add('fa-solid');
} else {
masterPlay.classList.remove('fa-circle-pause');
masterPlay.classList.add('fa-circle-play');
}
}
function playSong(key) {
const song = songs[key];
if (currentSongKey === key) {
if (audio.paused) {
audio.play();
updateAllCardIcons(key, true);
updateMasterPlay(true);
} else {
audio.pause();
updateAllCardIcons(key, false);
updateMasterPlay(false);
}
} else {
currentSongKey = key;
audio.src = song.src;
songTitle.innerText = song.title;
songArtist.innerText = song.artist;
songImg.src = song.img;
songImg.style.opacity = "1";
resetAllIcons();
audio.play();
updateAllCardIcons(key, true);
updateMasterPlay(true);
}
}
const allPlayButtons = document.querySelectorAll('.play-btn');
allPlayButtons.forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
const songKey = btn.getAttribute('data-song');
if (songKey) playSong(songKey);
});
});
masterPlay.addEventListener('click', () => {
// If master play is clicked but no song was selected yet, default to chill
if (!currentSongKey) {
playSong('chill');
return;
}
if (audio.paused) {
audio.play();
updateAllCardIcons(currentSongKey, true);
updateMasterPlay(true);
} else {
audio.pause();
updateAllCardIcons(currentSongKey, false);
updateMasterPlay(false);
}
});
volumeSlider.addEventListener('input', (e) => {
audio.volume = e.target.value / 100;
});
// Audio Events
audio.addEventListener('pause', () => {
updateMasterPlay(false);
if (currentSongKey) updateAllCardIcons(currentSongKey, false);
});
audio.addEventListener('play', () => {
updateMasterPlay(true);
if (currentSongKey) updateAllCardIcons(currentSongKey, true);
});
const progressBar = document.getElementById('progress-bar');
const currentTimeSpan = document.getElementById('current-time');
const totalDurationSpan = document.getElementById('total-duration');
// Format time mm:ss
function formatTime(seconds) {
if (isNaN(seconds)) return "0:00";
const min = Math.floor(seconds / 60);
const sec = Math.floor(seconds % 60);
return `${min}:${sec < 10 ? '0' : ''}${sec}`;
}
// Update max duration when metadata loads
audio.addEventListener('loadedmetadata', () => {
progressBar.max = audio.duration;
totalDurationSpan.innerText = formatTime(audio.duration);
});
// Update slider as song plays
audio.addEventListener('timeupdate', () => {
progressBar.value = audio.currentTime;
currentTimeSpan.innerText = formatTime(audio.currentTime);
});
// Seek when user changes slider
progressBar.addEventListener('input', () => {
audio.currentTime = progressBar.value;
});
});