-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
136 lines (114 loc) · 4.63 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
let songIndex = 0;
let playBtn = document.getElementById("playBtn");
let gif = document.getElementById("gif");
let masterSongName = document.getElementById("masterSongName");
let myProgressBar = document.getElementById("myProgressBar")
let audioElement = new Audio('songs/1.mp3');
let songItem = Array.from(document.getElementsByClassName('songItem'))
let playIcon = Array.from(document.getElementsByClassName("playIcon"))
// let songItem = document.getElementsByClassName('songItem')
// audioElement.play();
let songsArray = [
{ songName: "Kala chasma", filepath: "songs/1.mp3", coverpath: "covers/1.jpg" },
{ songName: "Mauja Hi Mauja", filepath: "songs/2.mp3", coverpath: "covers/2.jpg" },
{ songName: "Hawa Banke", filepath: "songs/3.mp3", coverpath: "covers/3.jpg" },
{ songName: "Maan Meri Jaan", filepath: "songs/4.mp3", coverpath: "covers/4.jpg" },
{ songName: "I Love You", filepath: "songs/5.mp3", coverpath: "covers/5.jpg" },
{ songName: "Rab ne bana di Jodi", filepath: "songs/6.mp3", coverpath: "covers/6.jpg" },
{ songName: "Soni de Nakhre", filepath: "songs/7.mp3", coverpath: "covers/7.jpg" }
]
//
//changing the cover and song with the use of js and here forEach is a loop which will work on array
songItem.forEach((element, i) => {
element.getElementsByTagName('img')[0].src = songsArray[i].coverpath;
element.getElementsByClassName('songName')[0].innerText = songsArray[i].songName
})
// handle play/pause events
playBtn.addEventListener("click", () => {
if (audioElement.paused || audioElement.currentTime <= 0) {
audioElement.play()
playBtn.classList.add("fa-pause-circle");
playBtn.classList.remove("fa-play-circle");
gif.style.opacity = 1;
}
else {
audioElement.pause()
playBtn.classList.remove("fa-pause-circle");
playBtn.classList.add("fa-play-circle");
gif.style.opacity = 0;
}
})
// updating the seekbar
audioElement.addEventListener("timeupdate", () => {
// console.log("timeupdate");
let progress = parseInt((audioElement.currentTime / audioElement.duration)*100);
myProgressBar.value = progress;
})
myProgressBar.addEventListener("change", () => {
audioElement.currentTime = myProgressBar.value * audioElement.duration / 100;
})
const makeAllPlays = () => {
playIcon.forEach((element) => {
element.classList.remove('fa-pause-circle')
element.classList.add('fa-play-circle')
})
}
playIcon.forEach((element) => {
element.addEventListener("click", (e) => {
makeAllPlays();
// console.log(e.target)
songIndex = parseInt(e.target.id);
if (audioElement.paused) {
e.target.classList.add('fa-pause-circle')
e.target.classList.remove('fa-play-circle')
audioElement.src = `songs/${songIndex + 1}.mp3`;
masterSongName.innerText = songsArray[songIndex].songName;
audioElement.currentTime =0;
audioElement.play();
playBtn.classList.add("fa-pause-circle");
playBtn.classList.remove("fa-play-circle");
gif.style.opacity = 1;
}
else{
e.target.classList.add('fa-play-circle')
e.target.classList.remove('fa-pause-circle')
audioElement.src = `songs/${songIndex + 1}.mp3`;
masterSongName.innerText = songsArray[songIndex].songName;
audioElement.currentTime = 0;
audioElement.pause();
playBtn.classList.add("fa-play-circle");
playBtn.classList.remove("fa-pause-circle");
gif.style.opacity = 0;
}
})
})
document.getElementById('next').addEventListener('click', () => {
if (songIndex >= 7) {
songIndex = 0;
}
else {
songIndex += 1;
}
audioElement.src = `songs/${songIndex + 1}.mp3`;
masterSongName.innerText = songsArray[songIndex].songName;
audioElement.currentTime = 0;
audioElement.play();
playBtn.classList.add("fa-pause-circle");
playBtn.classList.remove("fa-play-circle");
gif.style.opacity = 1;
})
document.getElementById('prev').addEventListener("click", () => {
if (songIndex <= 0) {
songIndex = 7;
}
else {
songIndex -= 1;
}
audioElement.src = `songs/${songIndex + 1}.mp3`;
masterSongName.innerText = songsArray[songIndex].songName;
audioElement.currentTime = 0;
audioElement.play();
playBtn.classList.add("fa-pause-circle");
playBtn.classList.remove("fa-play-circle");
gif.style.opacity = 1;
})