-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
139 lines (124 loc) · 3.96 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const image = document.querySelector('img');
const title = document.getElementById('title')
const artist = document.getElementById('artist');
const track = 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 tracks = [
{
name: 'jacinto-1',
displayName: 'Electric Chill Machine',
artist: 'Jacinto Design',
},
{
name: 'jacinto-2',
displayName: 'Seven Nation Army (Remix)',
artist: 'The White Stripes/Jacinto Design',
},
{
name: 'jacinto-3',
displayName: 'Goodnight, Disco Queen',
artist: 'Jacinto Design',
},
{
name: 'metric-1',
displayName: 'Front Row (Remix)',
artist: 'Metric/Jacinto Design',
}
]
//Check if playing
let isPlaying = false;
// Play
function playTrack() {
isPlaying = true;
playBtn.classList.replace('fa-play', 'fa-pause');
playBtn.setAttribute('title', 'Pause');
track.play();
}
function pauseTrack() {
isPlaying = false;
playBtn.classList.replace('fa-pause', 'fa-play');
playBtn.setAttribute('title', 'Play');
track.pause();
}
//Update DOM
function loadTrack(o) {
artist.textContent = o.artist;
title.textContent = o.displayName;
track.src = `music/${o.name}.mp3`;
image.src = `img/${o.name}.jpg`;
}
let trackIndex = 0;
let totalTracks = tracks.length;
//Previous Song
function prevTrack() {
trackIndex--;
if (trackIndex === -1) {
trackIndex = totalTracks - 1;
}
loadTrack(tracks[trackIndex]);
playTrack();
}
//Next Song
function nextTrack() {
trackIndex++;
if (trackIndex === totalTracks) {
trackIndex = 0;
}
loadTrack(tracks[trackIndex]);
playTrack();
}
// Update Progress Bar & Time
function 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 duration
const durationMinutes = Math.floor(duration / 60);
console.log('minutes', durationMinutes);
let durationSeconds = Math.floor(duration % 60);
if (durationSeconds < 10) {
durationSeconds = `0${durationSeconds}`;
}
console.log('seconds', durationSeconds);
//Delay switching duration ELement to avoid display NaN
if (durationSeconds) {
durationEl.textContent = `${durationMinutes}: ${durationSeconds}`;
}
// Calculate display for current
const currentMinutes = Math.floor(currentTime / 60);
let currentSeconds = Math.floor(currentTime % 60);
if (currentSeconds < 10) {
currentSeconds = `0${currentSeconds}`;
}
currentTimeEl.textContent = `${currentMinutes}: ${currentSeconds}`
}
}
//On Load - Select First Song
loadTrack(tracks[trackIndex]);
// Set Progress Bar
function setProgressBar(e) {
const width = this.clientWidth;
console.log(width);
const clickX = e.offsetX;
console.log(clickX);
const { duration } = track;
console.log(clickX / width);
console.log((clickX / width) * duration);
track.currentTime = (clickX / width) * duration;
}
//Play or Pause Event Listener
playBtn.addEventListener('click', () => (isPlaying ? pauseTrack() : playTrack()));
prevBtn.addEventListener('click', prevTrack);
nextBtn.addEventListener('click', nextTrack);
track.addEventListener('ended', nextTrack);
track.addEventListener('timeupdate', updateProgressBar);
progressContainer.addEventListener('click', setProgressBar);