-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
126 lines (121 loc) · 3.89 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
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
const image = document.querySelector('img');
const title = document.getElementById('title');
const artist = document.getElementById('artist');
const music = document.querySelector('audio');
const progressContainer = document.getElementById('progress-container');
const progress = document.getElementById('progress');
const currentTimeEl = document.getElementById('current-time');
const durationEl = document.getElementById('duration');
const prevBtn = document.getElementById('prev');
const playBtn = document.getElementById('play');
const nextBtn = document.getElementById('next');
// music
const songs = [
{
name: 'Pixie',
displayName: 'Pixie',
artist: 'slip.stream',
albumArt: 'william-christen-o4xVOHa3FXw-unsplash.jpg'
},
{
name:'Cyber Fields',
displayName: 'Cyber Fields',
artist: 'Spiros Maus',
albumArt: 'shambhavi-singh-GsJCCRWEsCE-unsplash.jpg'
},
{
name: 'Above And Below',
displayName: 'Above And Below',
artist: 'slip.stream',
albumArt: 'shambhavi-singh-uTUAflZAbIA-unsplash.jpg'
}
];
// Check if playing
let isPlaying = false;
// Play
const playSong = () => {
isPlaying = true;
// to show pause button after play click
playBtn.classList.replace('fa-play', 'fa-pause');
playBtn.setAttribute('title', 'Pause');
music.play();
}
// Pause
const pauseSong = () => {
isPlaying = false;
// play button shown after pause click
playBtn.classList.replace('fa-pause', 'fa-play');
playBtn.setAttribute('title', 'Play');
music.pause();
}
// Play or pause event listener
playBtn.addEventListener('click', () => (isPlaying ? pauseSong() : playSong()));
// Update DOM
const loadSong = song => {
title.textContent = song.displayName;
artist.textContent = song.artist;
music.src = `music/${song.name}.mp3`;
image.src = `img/${song.albumArt}`;
}
// Current song
let songIndex = 0;
// Previous song
const prevSong = () => {
songIndex--;
if (songIndex < 0) {
songIndex = songs.length - 1;
}
loadSong(songs[songIndex]);
playSong();
}
// Next song
const nextSong = () => {
songIndex++;
if (songIndex > songs.length - 1) {
songIndex = 0;
}
loadSong(songs[songIndex]);
playSong();
}
// On load - Select first song
loadSong(songs[songIndex]);
// Update progress bar & time by passing in event (e)
const updateProgressBar = (e) => {
if (isPlaying) {
const { duration, currentTime } = e.srcElement;
// update progress bar width
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
// calculate display for song duration
const durationMinutes = Math.floor(duration / 60);
let durationSeconds = Math.floor(duration % 60);
if (durationSeconds < 10) {
durationSeconds = `0${durationSeconds}`;
}
// delay switching duration element to avoid NaN showing
if (durationSeconds) {
durationEl.textContent = `${durationMinutes}:${durationSeconds}`;
}
// calculate display for currentTime
const currentMinutes = Math.floor(currentTime / 60);
let currentSeconds = Math.floor(currentTime % 60);
if (currentSeconds < 10) {
currentSeconds = `0${currentSeconds}`;
}
currentTimeEl.textContent = `${currentMinutes}:${currentSeconds}`;
}
}
//set progress bar
const setProgressBar = (e) => {
const width = e.srcElement.clientWidth;
const clickX = e.offsetX
const { duration } = music;
// width in seconds
music.currentTime = (clickX / width) * duration;
}
// Event Listeners
prevBtn.addEventListener('click', prevSong);
nextBtn.addEventListener('click', nextSong);
music.addEventListener('ended', nextSong);
music.addEventListener('timeupdate', updateProgressBar);
progressContainer.addEventListener('click', setProgressBar);