-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathscript.js
77 lines (68 loc) · 2.38 KB
/
script.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
// Initialize the 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 masterSongName = document.getElementById('masterSongName');
let songItems = Array.from(document.getElementsByClassName('songItem'));
let songs = [
// ... (your song data)
];
// Function to play the next song
const playNextSong = () => {
if (songIndex < songs.length - 1) {
songIndex++;
playSong();
} else {
songIndex = 0; // Loop back to the first song
playSong();
}
};
// Function to play a specific song
const playSong = () => {
audioElement.src = songs[songIndex].filePath;
masterSongName.innerText = songs[songIndex].songName;
audioElement.currentTime = 0;
audioElement.play();
masterPlay.classList.remove('fa-play-circle');
masterPlay.classList.add('fa-pause-circle');
gif.style.opacity = 1;
};
// Handle play/pause click
masterPlay.addEventListener('click', () => {
if (audioElement.paused || audioElement.currentTime <= 0) {
playSong();
} else {
audioElement.pause();
masterPlay.classList.remove('fa-pause-circle');
masterPlay.classList.add('fa-play-circle');
gif.style.opacity = 0;
}
});
// Listen to the 'ended' event for playing the next song
audioElement.addEventListener('ended', playNextSong);
// ... (your existing event listeners)
// Function to initialize song items
const initializeSongItems = () => {
songItems.forEach((element, i) => {
element.getElementsByTagName("img")[0].src = songs[i].coverPath;
element.getElementsByClassName("songName")[0].innerText = songs[i].songName;
element.getElementsByClassName("songItemPlay")[0].addEventListener('click', (e) => {
makeAllPlays();
songIndex = i;
e.target.classList.remove('fa-play-circle');
e.target.classList.add('fa-pause-circle');
playSong();
});
});
};
// Function to make all plays inactive
const makeAllPlays = () => {
Array.from(document.getElementsByClassName('songItemPlay')).forEach((element) => {
element.classList.remove('fa-pause-circle');
element.classList.add('fa-play-circle');
});
};
// Initialize song items
initializeSongItems();