-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
131 lines (95 loc) · 3.57 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
console.log("welcome to spotify");
//initializing the variables
let songindex=0;
let audioelement= new Audio('1.mp3');
// audioelement.play();
let play=document.getElementById('play');
let gif=document.getElementById('gif');
let progressbar=document.getElementById('progressbar');
let songitems= Array.from(document.getElementsByClassName('songitem'));
let songs=[
{songname:"dekha teri mast nighoan",filepath:"1.mp3",coverpath:"1.jpg"},
{songname:"ali ali",filepath:"2.mp3",coverpath:"2.jpg"},
{songname:"Haath mera thaam lo",filepath:"3.mp3",coverpath:"3.jpg"},
{songname:"Teri woh batein ",filepath:"4.mp3",coverpath:"4.jpg"},
{songname:"kamr patli",filepath:"5.mp3",coverpath:"5.jpg"},
{songname:"Boys attitude",filepath:"6.mp3",coverpath:"6.jpg"},
{songname:"hmare saath raghunath",filepath:"7.mp3",coverpath:"7.jpg"},
]
songitems.forEach((element,i) => {
// console.log(element,i);
element.getElementsByTagName('img')[0].src =songs[i].coverpath;
// element.getElementsByName('img')[0].src =song[i].coverpath;
element.getElementsByClassName('songname')[0].innerHTML = songs[i].songname;
});
// audioelement.play();
//
//handle play/pause click
play.addEventListener('click',()=>{
if(audioelement.paused || audioelement.currentTime <= 0){
audioelement.play();
play.classList.remove('fa-circle-play');
play.classList.add('fa-circle-pause');
gif.style.opacity=1;
}
else{
audioelement.pause();
play.classList.remove('fa-circle-pause');
play.classList.add('fa-circle-play');
gif.style.opacity=0;
}
})
//listen to events
audioelement.addEventListener('timeupdate',()=>{
//update seekbar
let progress = parseInt((audioelement.currentTime/audioelement.duration)*100);
progressbar.value = progress;
})
progressbar.addEventListener('change',()=>{
audioelement.currentTime = ((progressbar.value * audioelement.duration)/100);
})
const makeallplays = ()=>{
Array.from(document.getElementsByClassName('songitemplay')).forEach((element)=>{
element.classList.remove('fa-circle-pause');
element.classList.add('fa-circle-play');
})
}
Array.from(document.getElementsByClassName('songitemplay')).forEach((element)=>{
element.addEventListener('click',(e)=>{
// console.log(e.target);
makeallplays();
songindex = parseInt(e.target.id);
// console.log(index);
e.target.classList.remove('fa-circle-play');
e.target.classList.add('fa-circle-pause');
audioelement.src= `${songindex}.mp3`;
audioelement.currentTime=0;
audioelement.play();
play.classList.remove('fa-circle-play');
play.classList.add('fa-circle-pause');
})
})
document.getElementById('next').addEventListener('click',()=>{
if(songindex >= 7){
songindex =0; }
else{
songindex+=1;
}
audioelement.src= `${songindex}.mp3`;
audioelement.currentTime=0;
audioelement.play();
play.classList.remove('fa-circle-play');
play.classList.add('fa-circle-pause');
})
document.getElementById('previous').addEventListener('click',()=>{
if(songindex <= 0){
songindex = 0; }
else{
songindex-=1;
}
audioelement.src= `${songindex}.mp3`;
audioelement.currentTime=0;
audioelement.play();
play.classList.remove('fa-circle-play');
play.classList.add('fa-circle-pause');
})