-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
165 lines (152 loc) · 6 KB
/
app.js
File metadata and controls
165 lines (152 loc) · 6 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
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
let currentSong = new Audio();
let songs = [];
async function getSongs() {
try {
let response = await fetch("file:///C:/Users/Rufi_/Desktop/Small%20Projects/Spotify-Clone/songs/");
let data = await response.text();
// console.log(data); // This should log the array of song objects
let div = document.createElement('div');
div.innerHTML = data;
let as = div.getElementsByTagName("a");
for (let i = 0; i < as.length; i++) {
let element = as[i];
if (element.href.endsWith(".mp3")) {
songs.push(element.href);
}
}
return songs;
} catch (error) {
console.log("Error fetching songs:", error);
}
}
function convertSecondsToMinutes(seconds) {
// Calculate the minutes and remaining seconds
const minutes = Math.floor(seconds / 60);
const remainingSeconds = (seconds % 60).toFixed(0);
// Format the result as "minutes:seconds"
return `${minutes}:${remainingSeconds < 10 ? '0' + remainingSeconds : remainingSeconds}`;
}
function getImageSrcByAlt(altText) {
const images = document.querySelectorAll('img');
for (let img of images) {
if (img.alt === altText) {
return img.src;
}
}
return null; // Return null if no image with the given alt is found
}
function updateSong(songName) {
if (songName.endsWith('.mp3')) {
let finalsongname = songName.slice(songName.lastIndexOf('/') + 1);
finalsongname = finalsongname.replaceAll("%20", " ");
finalsongname = finalsongname.slice(0, finalsongname.indexOf('.'));
songName = finalsongname;
}
document.querySelector('#songImage').src = getImageSrcByAlt(songName);
document.querySelector('#title').innerHTML = `${songName}`;
}
const playMusic = (songName, image) => {
// http://127.0.0.1:5500/songs/Zaalima.mp3
console.log('file:///C:/Users/Rufi_/Desktop/Small%20Projects/Spotify-Clone/songs/' + songName + '.mp3');
currentSong.src = ('file:///C:/Users/Rufi_/Desktop/Small%20Projects/Spotify-Clone/songs/' + songName + '.mp3');
currentSong.play();
updateSong(songName);
}
let cards = document.querySelectorAll('.card');
function updateTitle() {
cards.forEach((card) => {
card.querySelector('.name').innerHTML = `${card.querySelector('img').alt}`;
})
}
async function main() {
let songs = await getSongs();
updateTitle();
let onlyonce=false;
// let prevbar=document.querySelector('.preview');
// let playbar=document.createElement('div');
cards.forEach(card => {
card.addEventListener('click', () => {
let song = card.querySelector('img');
playMusic(song.alt);
updateSong(song.alt);
document.querySelector('#play').src = "Spotify images/output.png";
if(!onlyonce){
document.querySelector('.preview').remove();
document.querySelector('.playbar-section').classList.add('preview');
onlyonce=true;
}
});
})
//play-pause the song
let play = document.querySelector('#play');
play.addEventListener('click', () => {
if (currentSong.paused) {
currentSong.play();
play.src = "Spotify images/output.png";
}
else {
currentSong.pause();
play.src = "Spotify images/player_icon3.png";
}
})
currentSong.addEventListener('timeupdate', () => {
if (!isNaN(currentSong.currentTime) && !isNaN(currentSong.duration)) {
document.querySelector('#time').innerHTML = `${convertSecondsToMinutes(currentSong.currentTime)}/${convertSecondsToMinutes(currentSong.duration)}`;
document.querySelector('.circle').style.left = (currentSong.currentTime / currentSong.duration) * 100 + "%";
}
})
document.querySelector('.seekbar').addEventListener('click', (e) => {
//very imp
let progress = (e.offsetX / e.target.getBoundingClientRect().width) * 100;
document.querySelector('.circle').style.left = `${progress}` + "%";
currentSong.currentTime = (currentSong.duration * progress) / 100;//song update due to bar
document.querySelector('.circle').style.borderColor = 'crimson';
})
let prev = document.querySelector('#previous');
let next = document.querySelector('#next');
prev.addEventListener('click', () => {
let currentSongName = currentSong.src.split('/').pop().split('.')[0];
// Find the index of the current song in the songs array
let index = songs.indexOf(`file:///C:/Users/Rufi_/Desktop/Small%20Projects/Spotify-Clone/songs/${currentSongName}.mp3`);
//finding next song
let nextsong = songs[(index - 1 + songs.length) % songs.length];
updateSong(nextsong);
currentSong.src = nextsong;
currentSong.play();
check();
})
next.addEventListener('click', () => {
let currentSongName = currentSong.src.split('/').pop().split('.')[0];
// Find the index of the current song in the songs array
let index = songs.indexOf(`file:///C:/Users/Rufi_/Desktop/Small%20Projects/Spotify-Clone/songs/${currentSongName}.mp3`);
//finding next song
let nextsong = songs[(index + 1) % songs.length];
updateSong(nextsong);
currentSong.src = nextsong;
currentSong.play();
check();
})
document.querySelector('#loop').addEventListener('click',()=>{
currentSong.currentTime=0;
if(!currentSong.played())
currentSong.play();
check();
})
document.querySelector('.range').addEventListener('change',(e)=>{
currentSong.volume=parseInt(e.target.value)/100;
})
<<<<<<< HEAD
function check(){
if (currentSong.paused) {
currentSong.play();
play.src = "Spotify images/output.png";
}
else {
currentSong.pause();
play.src = "Spotify images/player_icon3.png";
}
}
=======
>>>>>>> ea42912616775e1db7c142dffc3bc6664ebee0f6
}
main();