-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
285 lines (243 loc) · 10.5 KB
/
player.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Create a class to handle the music player
class MusicPlayer {
constructor() {
// Initialize the DOM elements
this.playButton = document.querySelector("#play");
this.pauseButton = document.querySelector("#pause");
this.forwardButton = document.querySelector("#fd");
this.reverseButton = document.querySelector("#rd")
this.rangeInput = document.querySelector("#vol");
this.durationInput = document.querySelector(".progress-bar");
this.currentTimeElement = document.querySelector(".curr-time");
this.totalTimeElement = document.querySelector(".tot-time");
this.singer = document.querySelector("#singer");
this.songName = document.querySelector("#song");
this.cover = document.querySelector("#cover");
let back = document.querySelector("#backout");
back.onclick = function() {
console.log("ss");
let b = document.querySelector(".back");
b.style.bottom = '0';
setTimeout(()=>{
window.location.href = 'index.html';
},500)
}
// Define the songs array
this.songs = [
{
singer: "Falak Shabir",
title: "Ijazat",
path: "./songs/song.mp3",
cover: "./assets/ijazat.jpeg",
pic : "./assets/falakshabir.jpg"
},
{
singer : "Karan Aujla",
title: "Softly",
path: "./songs/s2.mp3",
cover: "./assets/softly.jpeg",
pic : "./assets/KaranAujla.jpg"
},
{
singer : "Shubh",
title: "King Shit",
path: "./songs/KingShit.mp3",
cover: "./assets/shubh.jpg",
pic : "./assets/shubhpic.jpg"
}
];
// Set the initial song index and create an Audio object for the current song
this.currentSongIndex = 0;
this.currentSong = new Audio(this.songs[this.currentSongIndex].path);
console.log(this.currentSong)
}
// Play the current song
play() {
return new Promise((resolve, reject) => {
this.currentSong.play()
.then(() => {
// Update cover
this.cover.src = this.songs[this.currentSongIndex].cover;
this.singer.innerHTML = this.songs[this.currentSongIndex].singer;
// Change its title too
this.songName.innerHTML = this.songs[this.currentSongIndex].title;
// Change the background image
document.documentElement.style.setProperty('--bg-image', `url(${this.songs[this.currentSongIndex].pic})`);
this.playButton.style.display = "none";
this.pauseButton.style.display = "block";
resolve();
}).then(() => {
let cov = this.cover.src
applyAnimation(cov,'forwards');
})
});
}
// Pause the current song
async pause() {
try {
this.currentSong.pause();
this.playButton.style.display = "block";
this.pauseButton.style.display = "none";
} catch (error) {
console.error("Failed to pause the song:", error);
}
}
// Skip to the next song
async forward() {
try {
// Pause the current song and remove the timeupdate event listener
this.currentSong.pause();
this.currentSong.removeEventListener("timeupdate", () => this.updateProgress());
// Update the current song index and create a new Audio object for the next song
this.currentSongIndex = (this.currentSongIndex + 1) % this.songs.length;
this.currentSong = new Audio(this.songs[this.currentSongIndex].path);
// Add a loadedmetadata event listener to update the progress and re-add the timeupdate event listener
this.currentSong.addEventListener("loadedmetadata", () => {
this.updateProgress();
this.currentSong.addEventListener("timeupdate", () => this.updateProgress());
});
// Play the next song
await this.play().then(() => {
applyAnimation(cov,'forwards');
console.log("calling");
});
} catch (error) {
console.error("Failed to forward the song:", error);
}
}
// go to the prev song
async reverse() {
///only if there's at least one song
if (this.currentSongIndex >0) {
try {
// Check if there is at least one song in the playlist
// Pause the current song and remove the timeupdate event listener
this.currentSong.pause();
this.cover.src = '';
this.currentSong.removeEventListener("timeupdate", () => this.updateProgress());
// Update the current song index and create a new Audio object for the next song
this.currentSongIndex = (this.currentSongIndex - 1) % this.songs.length;
this.currentSong = new Audio(this.songs[this.currentSongIndex].path);
// Add a loadedmetadata event listener to update the progress and re-add the timeupdate event listener
this.currentSong.addEventListener("loadedmetadata", () => {
this.updateProgress();
this.currentSong.addEventListener("timeupdate", () => this.updateProgress());
});
// Play the next song
await this.play().then(()=>{
applyAnimation(cov,'reverse');
});
} catch (error) {
console.error("Failed to forward the song:", error);
}
}
else{
//if first set its to 0
this.currentSong.currentTime = 0;
}
}
// Set the volume of the current song
setVolume(volume) {
this.currentSong.volume = volume;
}
// Set the current time of the current song
setDuration(duration) {
this.currentSong.currentTime = duration;
}
// Update the progress bar and time display
updateProgress() {
const currentTime = this.currentSong.currentTime;
const totalTime = this.currentSong.duration;
// Calculate the current time in minutes and seconds
const currentMinutes = Math.floor(currentTime / 60);
const currentSeconds = Math.floor(currentTime % 60);
this.currentTimeElement.innerHTML = `${currentMinutes < 10 ? '0' : ''}${currentMinutes}:${currentSeconds < 10 ? '0' : ''}${currentSeconds}`;
// Calculate the total time in minutes and seconds
const totalMinutes = Math.floor(totalTime / 60);
const totalSeconds = Math.floor(totalTime % 60);
this.totalTimeElement.innerHTML = `${totalMinutes < 10 ? '0' : ''}${totalMinutes}:${totalSeconds < 10 ? '0' : ''}${totalSeconds}`;
// Calculate the progress percentage and update the duration input value
const progress = (currentTime / totalTime) * 100;
this.durationInput.value = progress;
}
// Initialize the music player by adding event listeners
initialize() {
this.playButton.addEventListener("click", () => this.play());
this.pauseButton.addEventListener("click", () => this.pause());
this.forwardButton.addEventListener("click", () => this.forward());
this.reverseButton.addEventListener("click",()=> this.reverse());
this.rangeInput.addEventListener("input", () => {
const volume = this.rangeInput.value / 100;
this.setVolume(volume);
});
this.durationInput.addEventListener("input", () => {
const duration = this.durationInput.value;
this.setDuration(duration);
});
this.currentSong.addEventListener("timeupdate", () => this.updateProgress());
}
}
function applyAnimation(cov, direction = 'forwards') {
// Start with the original cover
this.cover.src = cov;
// Apply a CSS transition to the cover's opacity
this.cover.style.transition = 'opacity 1s ease-in-out';
this.cover.style.opacity = 1;
setTimeout(() => {
try {
console.log("fade in calling");
// Add an animation to the singer element
this.singer.style.animation = `
fadeIn 1s ease-in-out ${direction},
hangingPosition 1s ease-in-out ${direction},
changesize 1s ease-in-out ${direction}
`;
// Define the fadeIn keyframes
const fadeInKeyframes = `
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
`;
// Define the hangingPosition keyframes
const hangingPositionKeyframes = `
@keyframes hangingPosition {
0% {
transform: translateX(50px);
}
100% {
transform: translateX(0);
}
}
`;
const changesizeframe = `
@keyframes changesize {
0% {
font-size: 0.8rem;
100%{
font-size:1.5rem;
}
}
`;
// Create a style element and append the keyframes to it
const styleElement = document.createElement('style');
styleElement.innerHTML = fadeInKeyframes + hangingPositionKeyframes + changesizeframe;
document.head.appendChild(styleElement);
// Fade out the cover
this.cover.style.opacity = 0;
// After the transition ends, remove the cover
this.cover.addEventListener('transitionend', () => {
this.cover.src = '';
}, { once: true });
} catch (e) {
console.log("e", e);
}
}, 2000)
}
// Create an instance of the MusicPlayer class
const musicPlayer = new MusicPlayer();
musicPlayer.initialize();