-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
137 lines (121 loc) · 4.64 KB
/
main.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
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
const audioPlayer = document.querySelector('audio');
const playBtn = document.getElementById('playBtn');
const pauseBtn = document.getElementById('pauseBtn');
const progressBar = document.getElementById('progress');
const volumeSlider = document.getElementById('volumeSlider');
const songImage = document.querySelector('.song-image img');
const songTitle = document.getElementById('songTitle');
const artistName = document.getElementById('artistName');
const songs = [
{
title: "Tip Tip Barsa Pani",
artist: "Song by Alka Yagnik and Udit Narayan",
audioSrc: "/assests/audio-songs/Tip Tip Barsa Pani 2.0 song.mp3",
albumArt: "/assests/title-images/tip tip barsa pani.jpg"
},
{
title: "Safari",
artist: "Song by Colombian singer J Balvin",
audioSrc: "/assests/audio-songs/Safari(PagalWorld).mp3",
albumArt: "/assests/title-images/safari.jpg"
},
{
title: "Aayi nai",
artist: "Singer Pawan Singh, Simran Choudhary",
audioSrc: "/assests/audio-songs/Aayi Nai Stree 2 320 Kbps.mp3",
albumArt: "/assests/title-images/Aayi-Nai.jpg"
},
{
title: "Aaj ki Raat",
artist: "Singer Sachin-Jigar, Madhubanti Bagchi",
audioSrc: "/assests/audio-songs/Aaj Ki Raat Stree 2 320 Kbps.mp3",
albumArt: "/assests/title-images/aaj ki raat.jpg"
},
{
title: "Khula Hai Maikhana",
artist: "Singer Nusrat Fateh Ali Khan",
audioSrc: "/assests/audio-songs/Khula Hai Maikhana (Remix) - Nusrat Fateh Ali Khan.mp3",
albumArt: "/assests/title-images/Nusrat Fateh Ali Khan.jpg"
}
];
let currentSongIndex = 0;
function loadSong(songIndex) {
const song = songs[songIndex];
audioPlayer.src = song.audioSrc;
songImage.src = song.albumArt;
songTitle.textContent = song.title;
artistName.textContent = song.artist;
console.log(`Loaded song: ${song.title}, Source: ${song.audioSrc}`);
const canPlay = audioPlayer.canPlayType('audio/jpg');
if (canPlay === '') {
console.warn(`Browser cannot play this file type for ${song.title}`);
}
}
playBtn.addEventListener('click', () => {
audioPlayer.play()
.then(() => {
playBtn.style.display = 'none';
pauseBtn.style.display = 'block';
updateProgress();
})
.catch(err => console.error('Error playing the audio:', err));
});
pauseBtn.addEventListener('click', () => {
audioPlayer.pause();
playBtn.style.display = 'block';
pauseBtn.style.display = 'none';
});
document.getElementById('nextBtn').addEventListener('click', () => {
currentSongIndex = (currentSongIndex + 1) % songs.length;
loadSong(currentSongIndex);
audioPlayer.play().then(() => {
playBtn.style.display = 'none';
pauseBtn.style.display = 'block';
}).catch(err => console.error('Error playing the next song:', err));
});
document.getElementById('prevBtn').addEventListener('click', () => {
currentSongIndex = (currentSongIndex - 1 + songs.length) % songs.length;
loadSong(currentSongIndex);
audioPlayer.play().then(() => {
playBtn.style.display = 'none';
pauseBtn.style.display = 'block';
}).catch(err => console.error('Error playing the previous song:', err));
});
audioPlayer.addEventListener('timeupdate', updateProgress);
function updateProgress() {
if (!isNaN(audioPlayer.duration)) {
const progress = (audioPlayer.currentTime / audioPlayer.duration) * 100;
progressBar.value = progress;
}
}
progressBar.addEventListener('input', () => {
const newTime = (progressBar.value / 100) * audioPlayer.duration;
audioPlayer.currentTime = newTime;
});
volumeSlider.addEventListener('input', () => {
audioPlayer.volume = volumeSlider.value;
});
loadSong(currentSongIndex);
audioPlayer.addEventListener('ended', () => {
currentSongIndex = (currentSongIndex + 1) % songs.length;
loadSong(currentSongIndex);
audioPlayer.play().then(() => {
playBtn.style.display = 'none';
pauseBtn.style.display = 'block';
}).catch(err => console.error('Error playing the next song after the current one ended:', err));
});
document.addEventListener('keydown', (event) => {
if (event.code === 'Space') {
event.preventDefault();
if (audioPlayer.paused) {
audioPlayer.play().then(() => {
playBtn.style.display = 'none';
pauseBtn.style.display = 'block';
}).catch(err => console.error('Error playing the audio:', err));
} else {
audioPlayer.pause();
playBtn.style.display = 'block';
pauseBtn.style.display = 'none';
}
}
});