-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (116 loc) · 4.82 KB
/
Copy pathscript.js
File metadata and controls
136 lines (116 loc) · 4.82 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
const currentSong = new Audio();
// Convert seconds to mm:ss
function secondsToMinutesSeconds(seconds) {
if (isNaN(seconds) || seconds < 0) return "00:00";
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.floor(seconds % 60);
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`;
}
// -----------------------------
// Albums array with your images and random songs
const albums = [
{ image: "pexels-atomlaborblog-844923.jpg", song: "car%arif%track1.mp3" },
{ image: "pexels-edurawpro-34933950.jpg", song: "random%20song2.mp3" },
{ image: "pexels-edurawpro-34933960.jpg", song: "strange%name3.mp3" },
{ image: "pexels-edurawpro-34933966.jpg", song: "song%204.mp3" },
{ image: "pexels-edurawpro-34933970.jpg", song: "track5.mp3" },
{ image: "pexels-farhan-ishraq-rudra-122465-375751.jpg", song: "my%20song6.mp3" },
{ image: "pexels-garrettmorrow-1649771.jpg", song: "random7.mp3" },
{ image: "pexels-kinkate-205926.jpg", song: "song8.mp3" },
{ image: "pexels-ola-dapo-1754561-3345882.jpg", song: "weird%name9.mp3" },
{ image: "pexels-sound-on-3394648.jpg", song: "track10.mp3" }
];
// -----------------------------
// Render albums
function renderAlbums() {
const container = document.querySelector(".albumContainer");
container.innerHTML = "";
albums.forEach((item, index) => {
container.innerHTML += `
<div class="albumCard" data-index="${index}">
<img src="./albums/${item.image}" alt="${item.image}">
<h3>${decodeURIComponent(item.image.replace(/\.(jpg|jpeg|png|gif)/i, ""))}</h3>
</div>`;
});
document.querySelectorAll(".albumCard").forEach(card => {
card.addEventListener("click", () => {
const index = card.dataset.index;
playMusic(albums[index]);
});
});
}
// Render songs list
function renderSongs() {
const ul = document.querySelector(".songList ul");
ul.innerHTML = "";
albums.forEach((item, index) => {
ul.innerHTML += `
<li class="songItem" data-index="${index}">
<span>${decodeURIComponent(item.song.replace(".mp3", ""))}</span>
</li>`;
});
document.querySelectorAll(".songItem").forEach(item => {
item.addEventListener("click", () => {
const index = item.dataset.index;
playMusic(albums[index]);
});
});
}
// Play music function
function playMusic(item) {
currentSong.src = `./albums/${item.song}`;
currentSong.play();
document.querySelector(".songinfo").innerText = decodeURIComponent(item.song.replace(".mp3", ""));
const playBtn = document.querySelector(".songbuttons img[src*='play']");
if (playBtn) playBtn.src = "./pause.svg";
const cover = document.querySelector(".currentCover");
if (cover) cover.src = `./albums/${item.image}`;
}
// Main initialization
function main() {
renderAlbums();
renderSongs();
const playBtn = document.querySelector(".songbuttons img[src*='play']");
const prevBtn = document.querySelector(".songbuttons img[src*='previous']");
const nextBtn = document.querySelector(".songbuttons img[src*='nextsong']");
// Play/Pause
playBtn?.addEventListener("click", () => {
if (currentSong.paused) {
currentSong.play();
playBtn.src = "./pause.svg";
} else {
currentSong.pause();
playBtn.src = "./play.svg";
}
});
// Previous/Next
prevBtn?.addEventListener("click", () => {
const index = albums.findIndex(a => a.song === currentSong.src.split("/").pop());
if (index > 0) playMusic(albums[index - 1]);
});
nextBtn?.addEventListener("click", () => {
const index = albums.findIndex(a => a.song === currentSong.src.split("/").pop());
if (index < albums.length - 1) playMusic(albums[index + 1]);
});
// Volume
const volumeInput = document.querySelector(".range input");
volumeInput?.addEventListener("input", e => {
currentSong.volume = e.target.value / 100;
});
// Seekbar
const seekbar = document.querySelector(".seekbar");
seekbar?.addEventListener("click", e => {
const percent = e.offsetX / e.target.clientWidth;
currentSong.currentTime = currentSong.duration * percent;
document.querySelector(".circle").style.left = percent * 100 + "%";
});
// Update song time
currentSong.addEventListener("timeupdate", () => {
const currentTime = secondsToMinutesSeconds(currentSong.currentTime);
const duration = secondsToMinutesSeconds(currentSong.duration);
document.querySelector(".songtime").innerText = `${currentTime} / ${duration}`;
document.querySelector(".circle").style.left = (currentSong.currentTime / currentSong.duration) * 100 + "%";
});
}
// Initialize
main();