-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
177 lines (129 loc) · 3.93 KB
/
script.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
const image= document.getElementById("photo");
const backImage= document.getElementById("full");
const title=document.querySelector('h2');
const artist=document.querySelector('h3');
const music=document.querySelector('audio');
const durationEl=document.getElementById("duration");
const currentTimeEl=document.getElementById("current-time");
const progressContainer=document.getElementById("progress-container");
const progressBar=document.getElementById("progress")
const prevBtn=document.getElementById("prev");
const playBtn=document.getElementById("play");
const nextBtn=document.getElementById("next");
const songs=[
{
name:'ArRahuman',
displayName:'Nadhiye Nadhiye',
artist:'Muthu',
color:'#000046'
},
{
name:'Believer',
displayName:'Imagine Drangons',
artist:'Muthu',
color:'rgb(253, 157, 0)'
},
{
name:'joker',
displayName:'Lai la la BGM',
artist:'Muthu',
color:' linear-gradient(to left, #0f9b0f, #000000)'
},
{
name:'Jack Sparrow',
displayName:'Pirates Of The Caribbean ',
artist:'Muthu',
color:'rgba(251, 6, 6, 0.979)'
},
{
name:'scam1992',
displayName:'Harshad Mehta',
artist:'Muthu',
color:'rgb(2, 161, 23)'
},
{
name:'Anirudh',
displayName:'Master-the-Blaster',
artist:'Muthu',
color:'rgba(0, 20,255, 0.9)'
},
]
let isPlaying=false;
function playSong(){
isPlaying=true;
playBtn.classList.replace('fa-play', 'fa-pause');
playBtn.setAttribute("title","pause");
music.play();
}
function pauseSong(){
isPlaying=false;
playBtn.classList.replace('fa-pause', 'fa-play');
playBtn.setAttribute("title","play")
music.pause()
}
playBtn.addEventListener('click',() => ( isPlaying ? pauseSong() : playSong()));
//update the dom
function loadSong(song){
artist.textContent=song.name;
title.textContent=song.displayName;
music.src=`music/${song.name}.mp3`
image.src=`img/${song.name}.jpg`;
document.body.style.background=song.color
}
let songIndex=0
loadSong(songs[songIndex]);
function prevSong(){
songIndex--;
if(songIndex<0){
songIndex=songs.length-1
}
console.log(songIndex);
loadSong(songs[songIndex]);
playSong();
}
function nextSong(){
songIndex++;
if(songIndex>songs.length-1){
songIndex=0
}
console.log(songIndex);
loadSong(songs[songIndex]);
playSong();
}
function updateProgress(e){
if (isPlaying) {
const { duration, currentTime } = e.srcElement;
// update progress bar width
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
// Calculate display for duration
const durationMinutes = Math.floor(duration / 60);
let durationSeconds = Math.floor(duration % 60);
if (durationSeconds < 10) {
durationSeconds = `0${durationSeconds}`;
}
// to avoid NaN
if (durationSeconds) {
durationEl.textContent = `${durationMinutes}:${durationSeconds}`;
}
// Calculate display for currentTime
const currentMinutes = Math.floor(currentTime / 60);
let currentSeconds = Math.floor(currentTime % 60);
if (currentSeconds < 10) {
currentSeconds = `0${currentSeconds}`;
}
currentTimeEl.textContent = `${currentMinutes}:${currentSeconds}`;
}
}
function setProgressBar(e){
const width =this.clientWidth;
console.log('width',width);
const clickX=e.offsetX;
console.log('clickX',clickX);
const { duration }=music;
music.currentTime=(clickX/width)*duration;
}
prevBtn.addEventListener('click',prevSong);
nextBtn.addEventListener('click',nextSong);
music.addEventListener('timeupdate',updateProgress)
progressContainer.addEventListener("click",setProgressBar);