-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusic.js
121 lines (107 loc) · 3.34 KB
/
Music.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
let progress = document.getElementById('progress');
let song = document.getElementById('song');
let ctrlIcon = document.getElementById('ctrlIcon');
let songImg = document.getElementById('songImg');
let songList = document.getElementById('songList');
let currentSongIndex = 0;
let currentListItem = null;
const playlist = [
{
title: 'Asma ul Husna',
artist: 'Atif Aslam | Coke Studio Special',
source: '/Audio/Asma-ul-Husna.mp3',
image: '/Images/Cover1.jpg'
},
{
title: 'The Beauty of Existence',
artist: 'Muhammad Al Muqit',
source: '/Audio/The Beauty of Existence.m4a',
image: '/Images/Cover2.jpg'
},
{
title: 'My Hope',
artist: 'Muhammad al Muqit',
source: '/Audio/My hope.m4a',
image: '/Images/Cover3.jpg'
},
{
title: "Hasn't Death Called You?",
artist: 'Nasheed by Mashary Rashed Al Afasy',
source: "/Audio/Hasn't Death Called You.m4a",
image: '/Images/Cover4.jpg'
},
];
loadSong();
song.onloadedmetadata = function () {
progress.max = song.duration;
progress.value = song.currentTime;
song.play();
ctrlIcon.classList.add('fa-pause');
ctrlIcon.classList.remove('fa-play');
highlightCurrentSong();
}
function loadSong() {
let currentSong = playlist[currentSongIndex];
song.src = currentSong.source;
songImg.src = currentSong.image;
document.querySelector('h1').textContent = currentSong.title;
document.querySelector('p').textContent = currentSong.artist;
}
function playPause() {
if (ctrlIcon.classList.contains('fa-pause')) {
song.pause();
ctrlIcon.classList.remove('fa-pause');
ctrlIcon.classList.add('fa-play');
} else {
song.play();
ctrlIcon.classList.add('fa-pause');
ctrlIcon.classList.remove('fa-play');
}
}
setInterval(() => {
progress.value = song.currentTime;
}, 500);
progress.onchange = function () {
song.play();
song.currentTime = progress.value;
ctrlIcon.classList.add('fa-pause');
ctrlIcon.classList.remove('fa-play');
}
function playNext() {
currentSongIndex = (currentSongIndex + 1) % playlist.length;
loadSong();
highlightCurrentSong();
}
function playPrev() {
currentSongIndex = (currentSongIndex - 1 + playlist.length) % playlist.length;
loadSong();
highlightCurrentSong();
}
function highlightCurrentSong() {
if (currentListItem) {
currentListItem.classList.remove('highlight');
}
currentListItem = songList.children[currentSongIndex];
currentListItem.classList.add('highlight');
}
document.addEventListener('DOMContentLoaded', function () {
const menuBtn = document.getElementById('menuBtn');
const menu = document.getElementById('menu');
menuBtn.addEventListener('click', function () {
menu.classList.toggle('show-menu');
});
playlist.forEach((song, index) => {
let listItem = document.createElement('li');
listItem.textContent = song.title;
listItem.addEventListener('click', () => {
currentSongIndex = index;
loadSong();
song.play();
ctrlIcon.classList.add('fa-pause');
ctrlIcon.classList.remove('fa-play');
highlightCurrentSong();
});
songList.appendChild(listItem);
});
highlightCurrentSong();
});