-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (163 loc) · 6.86 KB
/
Copy pathscript.js
File metadata and controls
181 lines (163 loc) · 6.86 KB
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
let currentSongs = new Audio();
let songs;
let currfolder;
function secondsToMinutesSeconds(seconds) {
if (isNaN(seconds) || seconds < 0) {
return "Invalid input";
}
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(remainingSeconds).padStart(2, '0');
return `${formattedMinutes}:${formattedSeconds}`;
}
async function getSongs(folder) {
currfolder = folder;
let a = await fetch(`/${folder}/`)
let response = await a.text();
let div = document.createElement("div")
div.innerHTML = response;
let as = div.getElementsByTagName("a")
songs = []
for (let index = 0; index < as.length; index++) {
const element = as[index];
if (element.href.endsWith(".mp3")) {
songs.push(element.href.split(`/${folder}/`)[1])
}
}
//get the list of all songs
let songUL = document.querySelector(".songlist").getElementsByTagName("ul")[0]
songUL.innerHTML = ""
for (const song of songs) {
songUL.innerHTML = songUL.innerHTML + `<li>
<img src="/assets/music.svg" alt="">
<div class="songinfo">
<div>${song.replaceAll("%20", " ")}</div>
<div>Satya</div>
</div>
<div class="playnow">
<span>Play Now</span>
<img src="/assets/play1.svg" alt="">
</div> </li>`;
}
//attach an eventlistner to each song
Array.from(document.querySelector(".songlist").getElementsByTagName("li")).forEach(e => {
e.addEventListener("click", element => {
console.log(e.querySelector(".songinfo").firstElementChild.innerHTML)
playMusic(e.querySelector(".songinfo").firstElementChild.innerHTML.trim())
})
})
return songs
}
const playMusic = (track, pause = false) => {
currentSongs.src = `/${currfolder}/` + track
if (!pause) {
currentSongs.play()
play.src = "/assets/pause.svg"
}
document.querySelector(".songinfo1").innerHTML = decodeURI(track)
document.querySelector(".songtime").innerHTML = "00:00 / 00:00"
}
async function displayAlbums() {
let a = await fetch("/songs/")
let response = await a.text();
let div = document.createElement("div")
div.innerHTML = response;
let anchors = div.getElementsByTagName("a")
let cardcontainer = document.querySelector(".cardmain")
let array = Array.from(anchors)
for (let index = 0; index < array.length; index++) {
const e = array[index];
if (e.href.includes("/songs")) {
let folder = e.href.split("/").slice(-2)[0]
let a = await fetch(`songs/${folder}/info.json`)
let response = await a.json();
cardcontainer.innerHTML = cardcontainer.innerHTML + ` <div data-folder="${folder}" class="card">
<div class="play"><img src="/assets/play.svg" alt=""></div>
<img src="/songs/${folder}/img1.jpeg" alt="">
<h2>${response.title}</h2>
<p>${response.description}</p>
</div>`
}
}
//load the playlist whenever card is clicked
Array.from(document.getElementsByClassName("card")).forEach(e => {
e.addEventListener("click", async item => {
songs = await getSongs(`songs/${item.currentTarget.dataset.folder}`)
playMusic(songs[0])
})
})
}
async function main() {
await getSongs("songs/2Hindi")
playMusic(songs[0], true)
//display all the albums
displayAlbums()
//attach an eventlistner to play, next and previous
play.addEventListener("click", () => {
if (currentSongs.paused) {
currentSongs.play()
play.src = "/assets/pause.svg"
}
else {
currentSongs.pause()
play.src = "/assets/play1.svg"
}
})
//listen for timeupdate event
currentSongs.addEventListener("timeupdate", () => {
console.log(currentSongs.currentTime, currentSongs.duration);
document.querySelector(".songtime").innerHTML = `${secondsToMinutesSeconds(currentSongs.currentTime)}:${secondsToMinutesSeconds(currentSongs.duration)}`
document.querySelector(".circle").style.left = (currentSongs.currentTime / currentSongs.duration) * 100 + "%";
})
//Add an event listner to sickbar
document.querySelector(".sickbar").addEventListener("click", e => {
let percent = (e.offsetX / e.target.getBoundingClientRect().width) * 100;
document.querySelector(".circle").style.left = percent + "%";
currentSongs.currentTime = ((currentSongs.duration) * percent) / 100
})
//add an eventlistner to hamburger
document.querySelector(".ham").addEventListener("click", () => {
document.querySelector(".left").style.left = "0"
})
//add an eventlistner to close
document.querySelector(".close").addEventListener("click", () => {
document.querySelector(".left").style.left = "-120%"
})
//add eventlistner to previous and next
previous.addEventListener("click", () => {
currentSongs.pause()
console.log("Previous clicked")
let index = songs.indexOf(currentSongs.src.split("/").slice(-1)[0])
if ((index - 1) >= 0) {
playMusic(songs[index - 1])
}
})
next.addEventListener("click", () => {
currentSongs.pause()
console.log("Next clicked")
let index = songs.indexOf(currentSongs.src.split("/").slice(-1)[0])
if ((index + 1) < songs.length) {
playMusic(songs[index + 1])
}
})
//add event to volume
document.querySelector(".range").getElementsByTagName("input")[0].addEventListener("change", (e) => {
console.log(e, e.target, e.target.value)
currentSongs.volume = parseInt(e.target.value) / 100
})
//add eventlistner to the volume for mute
document.querySelector(".sound>img").addEventListener("click", e => {
if (e.target.src.includes("/assets/volume.svg")) {
e.target.src = e.target.src.replace("/assets/volume.svg", "/assets/mute.svg")
currentSongs.volume = 0;
document.querySelector(".range").getElementsByTagName("input")[0].value = 0;
}
else {
e.target.src = e.target.src.replace("/assets/mute.svg", "/assets/volume.svg")
currentSongs.volume = .10;
document.querySelector(".range").getElementsByTagName("input")[0].value = 10;
}
})
}
main()