-
Notifications
You must be signed in to change notification settings - Fork 0
/
music-app.js
195 lines (143 loc) · 3.73 KB
/
music-app.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
let leftbtn=document.querySelector(".leftbtn")
let righttbtn=document.querySelector(".rightbtn")
let playbtn=document.querySelector(".playbtn")
let pausebtn=document.querySelector(".pausebtn")
let piccontainer=document.querySelector(".pic")
let singer=document.querySelector(".singername")
let audio=document.querySelector(".audio")
// Timers
let progresscontainer=document.querySelector('.progresscontainer')
let starttime=document.querySelector('.starttime')
let endtime=document.querySelector('.endtime')
let pics=[
{id:1,img:'pics/images(1).jpeg'},
{id:2,img:'pics/images.jpeg'},
{id:3,img:'pics/images3.jpg'},
];
let picindex=0
let songs=['music/music1.mp3',"music/music2.mp3",'music/music3.mp3'];
let singername=["Tom","oggy","simon"];
let singerindex=0;
let songindex=0;
const showpic=(index)=>{
piccontainer.innerHTML=`
<img src=${pics[index].img} alt=${picindex+1} class="songimg">
`
//Will do load corresponding songs...
loadmusic(songs[index]);
// Will load corresponding sinernames...
showsingername(singername[singerindex])
}
const showsingername=(name)=>{
singer.innerHTML=`${name}`
}
const loadmusic=(song)=>{
audio.src=song
}
function playsong(){
audio.play()
}
function pausesong(){
audio.pause()
}
function prevsong(){
songindex--
if(songindex<0){
songindex=songs.length-1;
}
else{
loadmusic[songs[songindex]]
}
playsong()
}
function nextsong()
{
songindex++
if(songindex>songs.length-1){
songindex=0
} loadmusic[songs[songindex]]
pausesong()
}
leftbtn.addEventListener("click",()=>{
//singer name
singerindex--
if(singerindex>0){
singerindex--
showsingername(singername[singerindex])
}
else{
singerindex=singername.length-1;
showsingername(singername[singerindex])
}
//music
prevsong()
// check if pic len is 0
if(picindex>0){
picindex--
showpic(picindex)
}
else{
picindex=pics.length-1;
showpic(picindex)
}
})
righttbtn.addEventListener("click",()=>{
singerindex++
if(singerindex<singername.length-1){
singerindex++
showsingername(singername[singerindex])
}
else{
singerindex=0;
showsingername(singername[singerindex])
}
nextsong()
if(picindex<pics.length-1){
picindex++
showpic(picindex)
}
else{
picindex=0
showpic(picindex)
}
})
playbtn.addEventListener("click",()=>{
//1.music will get played...
//audio.play()
playsong()
//2.play btn will get hide
playbtn.classList.add('none');
playbtn.classList.remove('show');
//3.pause btn will get displayed..
pausebtn.classList.add('show')
pausebtn.classList.remove('none');
})
pausebtn.addEventListener("click",()=>{
//1.music will get played...
//audio.pause()
pausesong()
//2.play btn will get show
playbtn.classList.add('show');
playbtn.classList.remove('none');
//3.pause btn will hide..
pausebtn.classList.add('none')
pausebtn.classList.remove('show')
})
function updatetime(e) {
progresscontainer.style.display='block'
let endtimevalue = e.srcElement.duration;
let starttimevalue = e.srcElement.currentTime;
let formatTime = (time) => {
let minutes = Math.floor(time / 60);
let seconds = Math.floor(time % 60);
if (seconds < 10) seconds = "0" + seconds;
return minutes + ":" + seconds;
};
// Update the text content
endtime.textContent = formatTime(endtimevalue);
starttime.textContent = formatTime(starttimevalue);
}
audio.addEventListener('timeupdate', updatetime);
//when our muics got ended
audio.addEventListener('ended',nextsong);
showpic(picindex)