-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (62 loc) · 1.84 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
const cartoonAudio = new Audio('./sound/Cartoon.mp3');
const forceAudio = new Audio('./sound/force.mp3');
const squeakyAudio = new Audio('./sound/squeaky.mp3');
const hopeAudio = new Audio('./sound/hope.mp3');
const janjiAudio = new Audio('./sound/Janji.mp3');
// selecting elements
const prevBtn = document.querySelector('.previous');
const playBtn = document.querySelector('.play-pause');
const nextBtn = document.querySelector('.next');
const songName = document.querySelector('.song-name');
const playPauseIcon = document.querySelector('#play-pause-icon');
const songs = [
{ ele: hopeAudio, audioName: 'Hope by NCS' },
{ ele: forceAudio, audioName: 'Force by NCS' },
{ ele: cartoonAudio, audioName: 'Cartoon by NCS' },
{ ele: janjiAudio, audioName: 'Janji by NCS' },
{ ele: squeakyAudio, audioName: 'Squeaky Sound' },
];
for(const song of songs) {
song.ele.addEventListener('ended', ()=> {
updateSong('next');
playPauseSong();
})
}
let current = 0;
let currentSong = songs[current].ele;
songName.textContent = songs[current].audioName;
playBtn.addEventListener('click',()=> {
playPauseSong();
})
nextBtn.addEventListener('click', () => {
updateSong('next');
playPauseSong();
});
prevBtn.addEventListener('click', () => {
updateSong('prev');
playPauseSong();
});
const updateSong = (action)=> {
currentSong.pause();
currentSong.currentTime = 0;
if(action === 'next'){
current++;
if(current > songs.length -1) current = 0;
}
if(action === 'prev'){
current--;
if(current < 0) current = songs.length - 1;
}
currentSong = songs[current].ele;
songName.textContent = songs[current].audioName;
}
const playPauseSong = ()=> {
if(currentSong.paused){
currentSong.play();
playPauseIcon.className = 'ph-bold ph-pause';
}
else {
currentSong.pause();
playPauseIcon.className = 'ph-bold ph-play';
}
}