-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (93 loc) · 3.67 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
console.log("Welcome to Spotify");
let songIndex = 0;
let audioElement = new Audio("songs/1.mp3");
let masterPlay = document.getElementById("masterPlay");
let myProgressBar = document.getElementById("myProgressBar");
let masterSongName = document.getElementById("masterSongName");
let gif = document.getElementById("gif");
let songItem = Array.from(document.getElementsByClassName("songItem"));
let songs=[{songName:"Music1", filePath:"songs/1.mp3", coverPath:"images/1.jpg"},
{songName:"Music2", filePath:"songs/2.mp3", coverPath:"images/2.jpg"},
{songName:"Music3", filePath:"songs/3.mp3", coverPath:"images/3.jpg"},
{songName:"Music4", filePath:"songs/4.mp3", coverPath:"images/4.jpg"},
{songName:"Music5", filePath:"songs/5.mp3", coverPath:"images/5.jpg"},
{songName:"Music6", filePath:"songs/6.mp3", coverPath:"images/6.jpg"},
{songName:"Music7", filePath:"songs/7.mp3", coverPath:"images/7.jpg"},
{songName:"Music8", filePath:"songs/8.mp3", coverPath:"images/8.jpg"},
{songName:"Music9", filePath:"songs/9.mp3", coverPath:"images/9.jpg"},
{songName:"Music10", filePath:"songs/10.mp3", coverPath:"images/10.jpg"}
];
songItem.forEach((elements,i) => {
elements.getElementsByTagName("img")[0].src = songs[i].coverPath;
elements.getElementsByClassName("songName")[0].innerText = songs[i].songName;
});
masterPlay.addEventListener("click",function(){
if(audioElement.paused || audioElement.currentTime<=0){
audioElement.play();
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
gif.style.opacity=1;
}
else{
audioElement.pause();
masterPlay.classList.remove("fa-pause-circle");
masterPlay.classList.add("fa-play-circle");
gif.style.opacity=0;
}
});
audioElement.addEventListener("timeupdate",function(){
progress = parseInt((audioElement.currentTime/audioElement.duration)*100);
myProgressBar.value = progress;
});
myProgressBar.addEventListener("change", function(){
audioElement.currentTime=myProgressBar.value * (audioElement.duration/100);
});
const makeAllPlays = ()=>{
Array.from(document.getElementsByClassName("songItemPlay")).forEach((element)=>{
element.classList.remove("fa-pause-circle");
element.classList.add("fa-circle-play");
})
}
Array.from(document.getElementsByClassName("songItemPlay")).forEach((element)=>{
element.addEventListener("click",(e)=>{
makeAllPlays();
songIndex = parseInt(e.target.id);
e.target.classList.remove("fa-circle-play");
e.target.classList.add("fa-pause-circle");
audioElement.src = `songs/${songIndex + 1}.mp3`;
masterSongName.innerText= songs[songIndex].songName;
audioElement.currentTime=0;
audioElement.play();
gif.style.opacity=1;
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
})
});
document.getElementById("next").addEventListener("click",function(){
if(songIndex>=9){
songIndex=0;
}
else{
songIndex+=1;
}
audioElement.src = `songs/${songIndex + 1}.mp3`
masterSongName.innerText= songs[songIndex].songName;
audioElement.currentTime=0;
audioElement.play();
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
});
document.getElementById("previous").addEventListener("click",function(){
if(songIndex<=0){
songIndex=0;
}
else{
songIndex-=1;
}
audioElement.src = `songs/${songIndex + 1}.mp3`
masterSongName.innerText= songs[songIndex].songName;
audioElement.currentTime=0;
audioElement.play();
masterPlay.classList.remove("fa-play-circle");
masterPlay.classList.add("fa-pause-circle");
});