-
Notifications
You must be signed in to change notification settings - Fork 0
/
jcode.js
107 lines (95 loc) · 3.78 KB
/
jcode.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
console.log("Welcome To Spotify");
// Initializing variables
let songIndex = 0;
let audioElement = new Audio('Songs/1.mp3');
let Masterplay = document.getElementById('Masterplay');
let myProgressBar = document.getElementById('myProgressBar');
let gif = document.getElementById('gif');
let songInfo = document.querySelector('.songInfo');
let songItems = Array.from(document.getElementsByClassName('songItem'));
let songs = [
{songName: "Village background Music", filePath: "Songs/2.mp3", coverPath: "Media/4.jfif"},
{songName: "Uplifting Feel Good", filePath: "Songs/3.mp3", coverPath: "Media/5.jfif"},
{songName: "Dancing in the stardust", filePath: "Songs/4.mp3", coverPath: "Media/6.jpg"},
{songName: "BG MUSIC", filePath: "Songs/5.mp3", coverPath: "Media/7.webp"},
{songName: "Beyond Horizons", filePath: "Songs/6.mp3", coverPath: "Media/8.webp"},
{songName: "Catholic Lay It Out", filePath: "Songs/7.mp3", coverPath: "Media/9.jfif"},
{songName: "tvari Tokyo Cafe", filePath: "Songs/1.mp3", coverPath: "Media/cover1.jpg"},
];
songItems.forEach((element, i) => {
element.getElementsByTagName('img')[0].src = songs[i].coverPath;
element.getElementsByClassName('songName')[0].innerText = songs[i].songName;
});
// Handle play/pause click
Masterplay.addEventListener('click', () => {
if (audioElement.paused || audioElement.currentTime <= 0) {
audioElement.play();
Masterplay.classList.remove('fa-play-circle');
Masterplay.classList.add('fa-pause-circle');
gif.style.opacity = 1;
} else {
audioElement.pause();
Masterplay.classList.remove('fa-pause-circle');
Masterplay.classList.add('fa-play-circle');
gif.style.opacity = 0;
}
});
// Listen to events
audioElement.addEventListener('timeupdate', () => {
console.log('timeupdate');
// Update seekbar
let progress = parseInt((audioElement.currentTime / audioElement.duration) * 100);
myProgressBar.value = progress;
});
myProgressBar.addEventListener('change', () => {
audioElement.currentTime = myProgressBar.value * audioElement.duration / 100;
});
const makeAllPlays = () => {
Array.from(document.getElementsByClassName("songItemPlay")).forEach((element) => {
element.classList.remove('fa-pause-circle');
element.classList.add('fa-play-circle');
});
};
const playSong = (index) => {
songIndex = index;
audioElement.src = songs[songIndex].filePath;
audioElement.currentTime = 0;
audioElement.play();
gif.style.opacity = 1;
Masterplay.classList.remove('fa-play-circle');
Masterplay.classList.add('fa-pause-circle');
songInfo.textContent = songs[songIndex].songName;
};
Array.from(document.getElementsByClassName("songItemPlay")).forEach((element, i) => {
element.addEventListener('click', (e) => {
makeAllPlays();
playSong(i);
e.target.classList.remove('fa-play-circle');
e.target.classList.add('fa-pause-circle');
});
});
// Function to play the next song
const nextSong = () => {
if (songIndex >= songs.length - 1) {
songIndex = 0;
} else {
songIndex += 1;
}
playSong(songIndex);
};
// Function to play the previous song or restart the current song
const prevSong = () => {
if (audioElement.currentTime > 3) {
audioElement.currentTime = 0;
} else {
if (songIndex <= 0) {
songIndex = songs.length - 1;
} else {
songIndex -= 1;
}
playSong(songIndex);
}
};
// Event listeners for rewind and fast forward buttons
document.querySelector('.fa-backward').addEventListener('click', prevSong);
document.querySelector('.fa-forward').addEventListener('click', nextSong);