-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathMusic.js
99 lines (84 loc) · 2.51 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
let play = document.getElementById("Play");
let previous = document.getElementById("Previous");
let next = document.getElementById("Next");
let audio = document.querySelector("audio");
let img = document.querySelector("img");
let title = document.getElementById("title");
let artist = document.getElementById("artist");
let songs = [
{
name: "Alone",
title: "Alone",
artist: "Alan Walker",
},
{
name: "Sugar",
title: "sugar & Brownies",
artist: "Dharia",
},
{
name: "Peaches",
title: "Peaches",
artist: "Justin Bieber",
},
];
let isplaying = false;
let playmusic = () => {
isplaying = true;
audio.play();
play.classList.replace('fa-play', 'fa-pause');
img.classList.add("anime");
};
let pausemusic = () => {
isplaying = false;
audio.pause();
play.classList.replace('fa-pause', 'fa-play');
};
play.addEventListener("click", () => {
if (isplaying == false) {
playmusic();
} else {
pausemusic();
};
});
const loadsong = (songs) => {
title.textContent = songs.title;
artist.textContent = songs.artist;
audio.src = "Music/" + songs.name + ".mp3";
img.src = "images/" + songs.name + ".jpg";
}
let songindex = 1;
const nextsong = () => {
songindex = (songindex + 1) % songs.length;
loadsong(songs[songindex]);
playmusic();
}
const prevsong = () => {
songindex = (songindex - 1 + songs.length) % songs.length;
loadsong(songs[songindex]);
playmusic();
}
next.addEventListener('click', nextsong);
previous.addEventListener('click', prevsong);
// Volume control
let volume_slider = document.querySelector('.volume_slider');
function setVolume() {
audio.volume = volume_slider.value / 100;
}
// Progress control
let progress_slider = document.querySelector('.progress_slider');
function setProgress() {
audio.currentTime = audio.duration * (progress_slider.value / 100);
}
// Update progress slider as the audio plays
audio.addEventListener('timeupdate', () => {
let progress = (audio.currentTime / audio.duration) * 100;
progress_slider.value = progress;
// Update progress slider's background to fill up as the song plays
let fillWidth = (audio.currentTime / audio.duration) * 100;
progress_slider.style.background = `linear-gradient(to right, #4CAF50 0%, #4CAF50 ${fillWidth}%, #ddd ${fillWidth}%, #ddd 100%)`;
});
// Update the audio time as the progress slider changes
progress_slider.addEventListener('input', () => {
setProgress();
});