-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
238 lines (185 loc) · 5.38 KB
/
index.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
const image = document.querySelector('img');
const title= document.getElementById('title');
const artist = document.getElementById('artist');
const music = document.querySelector('audio');
const progressContainer= document.getElementById('progress-container');
const progress = document.querySelector('#progress');
const currentTimeEl = document.getElementById('current-time');
const durationEl = document.getElementById('duration');
const prevBtn= document.getElementById('prev');
const playBtn= document.getElementById('play');
const nextBtn= document.getElementById('next');
//for playlist
const playlist = document.querySelector('.playlist');
let ul = document.createElement('ul');
const songs=[
{
name:'Escapism.',
displayName:'Escapism',
artist:'RAYA, 070 Shake'
},
{
name:'kiss me',
displayName:'kiss me',
artist:'bbygirl'
},
{
name:'favorite',
displayName:'favorite',
artist:'Isabel LaRosa'
},
{
name:'Valla Valla baby',
displayName:'Valla Valla baby',
artist:'Mr_cool'
},
{
name:'Look at my eyes',
displayName:'Look at my eyes',
artist:'Travis Scott'
},
{
name:'Everyday Normal Guy',
displayName:'Everyday Normal Guy',
artist:'Mr_cool'
},
{
name:'As it was',
displayName:'As it was',
artist:'Harry Styles'
},
{
name:'Romancham',
displayName:'Romancham',
artist:'Sushin shyam,Neha'
},
{
name:'Katchi Sera',
displayName:'Katchi Sera',
artist:'Sai Abhyankkar'
},
{
name:'Ethir Neechal',
displayName:'Ethir Neechal',
artist:'yo yo honey singh'
}
];
//Making a list to append according to array;
console.log(songs);
const listItem = songs.map(el=>{
return el.displayName;
});
console.log(listItem);
songs.map(e=>{
let item = document.createElement('li');
item.append(e.displayName);
ul.append(item);
}
);
playlist.append(ul);
//here making list ends
let index;
const indexCalculate=(name)=>{
songs.forEach((e,i)=>{
if(e.displayName==name){
index=i;
}
} );
};
ul.addEventListener('click',(e)=>{
let listItemName= e.target.textContent;
indexCalculate(listItemName);
console.log(index);
loadSong(songs[index]);
playSong();
});
//check if playing
let isPlaying=false;
let songIndex=0;
//play
const playSong = ()=>{
isPlaying=true;
playBtn.classList.replace('fa-play','fa-pause');
playBtn.setAttribute('title','Pause');
music.play();
};
//pause
const pauseSong = ()=>{
isPlaying=false;
playBtn.classList.replace('fa-pause','fa-play');
playBtn.setAttribute('title','Play');
music.pause();
};
//play or pause event listener
playBtn.addEventListener('click',()=>(isPlaying?pauseSong():playSong()));
const prevSong = () =>{
songIndex--;
if(songIndex<0){
songIndex=songs.length -1;
}
loadSong(songs[songIndex]);
playSong();
};
const nextSong = () =>{
songIndex++;
if(songIndex>songs.length-1){
songIndex=0;
}
loadSong(songs[songIndex]);
playSong();
};
prevBtn.addEventListener('click',prevSong);
nextBtn.addEventListener('click',nextSong);
//all songs list
//update DOM Songs
const loadSong=(song)=>{
title.textContent=song.displayName;
artist.textContent=song.artist;
music.src=`music/${song.name}.mp3`;
image.src= `images/${song.name}.jpg`;
};
//default music
const updateProgressBar = (e)=>{
if(isPlaying){
// console.log(e);
const{ duration,currentTime} = e.srcElement;
//console.log(duration,currentTime);
const progressPercent = (currentTime/duration)*100;
//console.log(progressPercent);
progress.style.width=`${progressPercent}%`;
//calculate display for duration
const durationMinutes = Math.floor(duration/60);
// console.log(durationMinutes);
let durationSeconds= Math.floor(duration%60);
if(durationSeconds<10){
durationSeconds=`0${durationSeconds}`;
};
//to delay updating time in DOM TO PREVENT NAN
if(durationSeconds){
durationEl.textContent=`${durationMinutes}:${durationSeconds}`;
}
//calculate display for current
const currentMinutes = Math.floor(currentTime/60);
//console.log(durationMinutes);
let currentSeconds= Math.floor(currentTime%60);
if(currentSeconds<10){
currentSeconds=`0${currentSeconds}`;
};
if(currentSeconds){
currentTimeEl.textContent=`${currentMinutes}:${currentSeconds}`;
}
}
}
const setProgressBar=(e)=>{
//console.log(e);
//console.log('clicked');
const width = e.srcElement.clientWidth;
const clickX = e.offsetX;
//console.log(width,clickX);
const { duration } = music;
//console.log(music.currentTime);
music.currentTime=(clickX/width)*duration;
};
music.addEventListener('timeupdate',updateProgressBar);
music.addEventListener('ended',nextSong);
progressContainer.addEventListener('click',setProgressBar);