-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
279 lines (233 loc) · 8.32 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const image = document.querySelector('img');
const title = document.getElementById('title');
const artist = document.getElementById('artist');
const audio = document.querySelector('audio');
const progressContainer = document.getElementById('progress-container');
const progress = document.getElementById('progress');
const currentTimeElement = document.getElementById('current-time');
const durationElement = document.getElementById('duration');
const buttonPrevious = document.getElementById('previous');
const buttonBackWard = document.getElementById('backWard');
const buttonPlay = document.getElementById('play');
const buttonForWard = document.getElementById('forWard');
const buttonNext = document.getElementById('next');
const playList = [
{
name: 'The Qur\'ān Changed Me',
displayName: 'The Qur\'ān Changed Me - القرآن غيرني',
artist: 'Khalid Mahjoub - خالد محجوب'
},
{
name: 'Sūraṫ an-Naḥl 63-69',
displayName: 'Sūraṫ an-Naḥl (The Bee) 63–69 - سورة النحل',
artist: 'AbdulBasit AbdusSamad - عبد الباسط عبد الصمد'
},
{
name: 'Sūraṫ al-Isrā\' 1-38',
displayName: 'Sūraṫ al-Isrā\' (The Night Journey) 1–38 - سورة الإسراء',
artist: 'Souleyman Al Mali - سليمان المالي'
},
{
name: 'Sūraṫ Ibrāhīm 42-48',
displayName: 'Sūraṫ Ibrāhīm (Abraham) 42–48 - سورة إبراهيم',
artist: 'Aftab Ahmed - أفطاب أحمد'
},
{
name: 'Asmā\' al-Ḥusná (Beautiful Names)',
displayName: 'Asmā\' al-Ḥusná (Beautiful Names) - أسماء الحسنى',
artist: 'Ahmed AlNufais - أحمد النفيس'
},
{
name: 'How Does One Call You%3F',
displayName: 'How Does One Call You? - كيف يناجيك',
artist: 'AbdiRashid Sufi - عبد الرشيد صوفي'
},
{
name: 'God is My Strength (Ash-Shāṭibiyyaḣ)',
displayName: 'God is My Strength (Ash-Shāṭibiyyaḣ) - بالله حولي (الشاطبية)',
artist: 'AbdiRashid Sufi - عبد الرشيد صوفي'
},
{
name: 'O God, ReWard Muḥammad for Us',
displayName: 'O God, ReWard Muḥammad for Us - اللهم جازي عنا محمدا',
artist: 'AbdiRashid Sufi - عبد الرشيد صوفي'
},
{
name: 'O He Who ReVeals the Beautiful',
displayName: 'O He Who ReVeals the Beautiful - يا من أظهر الجميل',
artist: 'AbdiRashid Sufi - عبد الرشيد صوفي'
},
{
name: 'How Can it EnCompass You%3F',
displayName: 'How Can it EnCompass You? - كيف يحيط بك',
artist: 'AbdiRashid Sufi - عبد الرشيد صوفي'
},
{
name: 'O God, O BeLoved...',
displayName: 'O God, O BeLoved... - اللهم يا حبيب',
artist: 'AbdiRashid Sufi - عبد الرشيد صوفي'
},
{
name: 'How Could the Palms Hold Back%3F',
displayName: 'How Could the Palms Hold Back? - كيف كفت الأكف',
artist: 'AbdiRashid Sufi - عبد الرشيد صوفي'
},
{
name: 'BeLoved of the Hearts',
displayName: 'BeLoved of the Hearts - حبيب القلوب',
artist: 'Osama Yousif | Khabbab Al-Amin - أسامة يوسف | خباب الأمين'
},
{
name: 'Know YourSelf',
displayName: 'Know YourSelf - أعرف نفسك',
artist: 'Anas Mohammeden | Osama Yousif - أنس محمدين | أسامة يوسف'
},
{
name: 'I Will Get Married',
displayName: 'I Will Get Married - حا أزوج',
artist: 'Anas Mohammeden | Osama Yousif - أنس محمدين | أسامة يوسف'
},
{
name: 'ForeRunners',
displayName: 'ForeRunners - سابقون',
artist: '`Abd ar-Raḥmān Aṣ-Ṣādiq - عبد الرحمن الصادق'
},
{
name: 'O Month of Fasting',
displayName: 'O Month of Fasting - يا شهر الصوم',
artist: 'Anas Mohammeden - أنس محمدين'
},
{
name: 'Original Man',
displayName: 'Original Man - زول المأصل',
artist: 'Khalid Mahjoub | Anas Mohammeden - خالد محجوب | أنس محمدين'
},
{
name: 'Echo of the PulPit',
displayName: 'Echo of the PulPit - صدى المحراب',
artist: 'Khalid Mahjoub - خالد محجوب'
}
];
let isPlaying = false;
let index = 0;
(async function preLoad() {
playList.forEach(({ name }) => {
(new Image()).src = `./images/${name}.jpg`;
new Audio(`./audio/${name}.mp3`);
});
})();
function upDatePositionState() {
navigator.mediaSession.setPositionState({
duration: audio.duration,
position: audio.currentTime
});
};
function upDateMetaData() {
const { name, displayName, artist } = playList[index];
navigator.mediaSession.metadata = new MediaMetadata({
title: displayName,
album: 'Axon4',
artist,
artwork: [{
src: `./images/${name}-512.jpg`,
sizes: '512x512',
type: 'image/jpg'
}]
});
upDatePositionState();
};
function playAudio() {
isPlaying = true;
navigator.mediaSession.playbackState = 'playing';
buttonPlay.classList.replace('fa-play', 'fa-pause');
buttonPlay.setAttribute('title', 'pause');
audio.play().then(upDateMetaData);
};
navigator.mediaSession.setActionHandler('play', playAudio);
function pauseAudio() {
isPlaying = false;
navigator.mediaSession.playbackState = 'paused';
buttonPlay.classList.replace('fa-pause', 'fa-play');
buttonPlay.setAttribute('title', 'play');
audio.pause();
};
navigator.mediaSession.setActionHandler('pause', pauseAudio);
buttonPlay.addEventListener('click', () => {isPlaying ? pauseAudio() : playAudio()});
function loadAudio(file) {
title.textContent = file.displayName;
artist.textContent = file.artist;
image.src = `./images/${file.name}.jpg`;
audio.src = `./audio/${file.name}.mp3`;
};
function skipBackWard(event) {
const skipTime = event.seekOffset || 10;
audio.currentTime = Math.max(audio.currentTime - skipTime, 0);
upDatePositionState();
};
navigator.mediaSession.setActionHandler('seekbackward', skipBackWard);
function skipForWard(event) {
const skipTime = event.seekOffset || 10;
audio.currentTime = Math.min(audio.currentTime + skipTime, audio.duration);
upDatePositionState();
};
navigator.mediaSession.setActionHandler('seekforward', skipForWard);
function previousAudio() {
index--;
if (index < 0) index = playList.length - 1;
loadAudio(playList[index]);
playAudio();
};
navigator.mediaSession.setActionHandler('previoustrack', previousAudio);
function nextAudio() {
index++;
if (index > playList.length - 1) index = 0;
loadAudio(playList[index]);
playAudio();
};
navigator.mediaSession.setActionHandler('nexttrack', nextAudio);
// function stopAudio() {
// alert('STOP');
// };
// navigator.mediaSession.setActionHandler('stop', stopAudio);
loadAudio(playList[index]);
function upDateProgressBar(event) {
if (isPlaying) {
const { duration, currentTime } = event.srcElement;
const progressPercentage = (currentTime / duration) * 100;
progress.style.width = `${progressPercentage}%`;
const durationMinutes = Math.floor(duration / 60);
let durationSeconds = Math.floor(duration % 60);
if (durationSeconds < 10) {
durationSeconds = `0${durationSeconds}`;
};
// delay to avoid 'NaN'
if (durationSeconds) {
durationElement.textContent = `${durationMinutes}:${durationSeconds}`;
};
const currentMinutes = Math.floor(currentTime / 60);
let currentSeconds = Math.floor(currentTime % 60);
if (currentSeconds < 10) {
currentSeconds = `0${currentSeconds}`;
};
currentTimeElement.textContent = `${currentMinutes}:${currentSeconds}`;
};
};
function setProgressBar(event) {
const width = this.clientWidth;
const clickX = event.offsetX;
const { duration } = audio;
audio.currentTime = (clickX / width) * duration;
};
function seek(event) {
if (event.fastSeek && 'fastSeek' in audio) audio.fastSeek(event.seekTime);
else audio.currentTime = event.seekTime;
upDatePositionState();
};
navigator.mediaSession.setActionHandler('seekto', seek);
buttonPrevious.addEventListener('click', previousAudio);
buttonBackWard.addEventListener('click', skipBackWard);
buttonForWard.addEventListener('click', skipForWard);
buttonNext.addEventListener('click', nextAudio);
audio.addEventListener('timeupdate', upDateProgressBar);
audio.addEventListener('ended', nextAudio);
progressContainer.addEventListener('click', setProgressBar);