-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
253 lines (212 loc) · 7.68 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const bar = document.getElementById("search-bar");
const search = document.getElementById("search-table");
const button = document.getElementById("search-button");
const warn = document.getElementById("warn");
const searched = document.getElementById("findes");
const musicContainer = document.querySelector(".music-container");
const player = document.getElementById("player");
const playBtn = document.getElementById("play");
const prevBtn = document.getElementById("prev");
const nextBtn = document.getElementById("next");
const audio = document.getElementById("audio");
const progress = document.getElementById("progress");
const progressContainer = document.getElementById("progress-container");
const songTitle = document.getElementById("title");
const artist = document.getElementById("artist");
const cover = document.getElementById("cover");
const currTime = document.querySelector("#currTime");
const durTime = document.querySelector("#durTime");
const title = document.title;
const songs = [{
name: "Khoda Kooshi",
artist: "Arta (Ft Mehrad Hidden)",
source: "https://dl.prymie.com/arta/music/Khoda_Kooshi.mp3"
},
{
name: "Maar",
artist: "Mehrad Hidden",
source: "https://www.appahang5.com/cdn/tracks-mp3/1397/08/2467164776613658871647766136159716477661364280.mp3"
}];
let findes;
/**
*
* @param {boolean} isTyping
* @param {Element} element
* @returns
*/
function play(isTyping = false, element = null) {
if (isTyping && element) {
const [value] = element.innerHTML.toString().split(" - ");
searched.classList.remove("typing");
findes = songs.filter(a => a.name.includes(value));
if (!findes || findes.length < 1) return warn.innerHTML = "! This music undefind";
warn.innerHTML = "";
searched.innerHTML = "";
search.value = "";
player.hidden = false;
} else {
const value = search.value;
if (!value) return warn.innerHTML = "! please fillout the table before click on button";
findes = songs.filter(a => a.name.toLowerCase().includes(value.toLowerCase()) || a.artist.toLowerCase().includes(value.toLowerCase()));
if (!findes || findes.length < 1) return warn.innerHTML = "! This music undefind";
warn.innerHTML = "";
search.value = "";
searched.innerHTML = "";
player.hidden = false;
};
let songIndex = findes.length < 1 ? 0 : findes.length - 1;
// Update song details
/**
*
* @param {songs[number]} song
* @returns
*/
function loadSong(song) {
songTitle.innerText = song.name;
artist.innerText = song.artist;
audio.src = `${song.source}`;
cover.src = `/images/${song.name}.jpg`;
}
// Play song
function playSong() {
if (songTitle.innerText === "") loadSong(findes[songIndex]);
musicContainer.classList.add("play");
playBtn.querySelector("i.fas").classList.remove("fa-play");
playBtn.querySelector("i.fas").classList.add("fa-pause");
audio.play();
}
// Pause song
function pauseSong() {
musicContainer.classList.remove("play");
playBtn.querySelector("i.fas").classList.add("fa-play");
playBtn.querySelector("i.fas").classList.remove("fa-pause");
audio.pause();
}
// Previous song
function prevSong() {
songIndex--;
if (songIndex < 0) {
songIndex = findes.length - 1;
}
loadSong(findes[songIndex]);
playSong();
}
// Next song
function nextSong() {
songIndex++;
if (songIndex > findes.length - 1) {
songIndex = 0;
}
loadSong(findes[songIndex]);
playSong();
}
// Update progress bar
function updateProgress(e) {
const { duration, currentTime } = e.srcElement;
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
}
// Set progress bar
function setProgress(e) {
const width = this.clientWidth;
const clickX = e.offsetX;
const duration = audio.duration;
audio.currentTime = (clickX / width) * duration;
}
//get duration & currentTime for Time of song
function DurTime(e) {
const { duration, currentTime } = e.srcElement;
var sec;
var sec_d;
// define minutes currentTime
let min = (currentTime == null) ? 0 :
Math.floor(currentTime / 60);
min = min < 10 ? "0" + min : min;
// define seconds currentTime
function get_sec(x) {
if (Math.floor(x) >= 60) {
for (var i = 1; i <= 60; i++) {
if (Math.floor(x) >= (60 * i) && Math.floor(x) < (60 * (i + 1))) {
sec = Math.floor(x) - (60 * i);
sec = sec < 10 ? "0" + sec : sec;
}
}
} else {
sec = Math.floor(x);
sec = sec < 10 ? "0" + sec : sec;
}
}
get_sec(currentTime, sec);
// change currentTime DOM
currTime.innerHTML = min + ":" + sec;
// define minutes duration
let min_d = (isNaN(duration) === true) ? "0" :
Math.floor(duration / 60);
min_d = min_d < 10 ? "0" + min_d : min_d;
function get_sec_d(x) {
if (Math.floor(x) >= 60) {
for (var i = 1; i <= 60; i++) {
if (Math.floor(x) >= (60 * i) && Math.floor(x) < (60 * (i + 1))) {
sec_d = Math.floor(x) - (60 * i);
sec_d = sec_d < 10 ? "0" + sec_d : sec_d;
}
}
} else {
sec_d = (isNaN(duration) === true) ? "0" :
Math.floor(x);
sec_d = sec_d < 10 ? "0" + sec_d : sec_d;
}
}
// define seconds duration
get_sec_d(duration);
// change duration DOM
durTime.innerHTML = min_d + ":" + sec_d;
};
// Event listeners
playBtn.addEventListener("click", () => {
const isPlaying = musicContainer.classList.contains("play");
if (isPlaying) {
pauseSong();
} else {
playSong();
}
});
// Change song
prevBtn.addEventListener("click", prevSong);
nextBtn.addEventListener("click", nextSong);
// Time/song update
audio.addEventListener("timeupdate", updateProgress);
// Click on progress bar
progressContainer.addEventListener("click", setProgress);
// Song ends
audio.addEventListener("ended", nextSong);
// Time of song
// audio.addEventListener("timeupdate", DurTime);
};
window.addEventListener("focus", () => {
document.title = title;
});
window.addEventListener("blur", () => {
document.title = "Came back I miss u :(";
});
search.addEventListener("change", () => {
const value = search.value;
if (value == "") return searched.innerHTML = "";
const typing = songs.filter(a => a.name.toLowerCase().includes(value.toLowerCase()) || a.artist.toLowerCase().includes(value.toLowerCase()));
warn.innerHTML = "";
searched.classList.add("typing");
searched.innerHTML = typing.map(a => `<li class="song">${a.name} - ${a.artist}</li>`).join("");
document.querySelectorAll(".song").forEach(element => {
element.addEventListener("click", () => play(true, element));
});
});
button.addEventListener("click", () => play());
/**
* @copyright
* Coded by Sobhan-SRZA (mr.sinre) | https://github.com/Sobhan-SRZA
* @copyright
* Work for Persian Caesar | https://dsc.gg/persian-caesar
* @copyright
* Please Mention Us "Persian Caesar", When Have Problem With Using This Code!
* @copyright
*/