-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
148 lines (120 loc) · 3.52 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
140
141
142
143
144
145
146
147
const audioContainer = document.getElementById('audio-container');
const playBtn = document.getElementById('play');
const prevBtn = document.getElementById('prev');
const nextBtn = document.getElementById('next');
const audio = document.getElementById('audio');
const progress = document.getElementById('progress');
const progressContainer = document.getElementById('progress-container');
const title = document.getElementById('title');
const cover = document.getElementById('cover');
// audio files
const files = ['elevate', 'evolution', 'littleidea'];
// Keep track of file
let fileIndex = 0;
/**
* Update the file details
* @param {string} file name of the audio file to update details of
*/
function loadFile(file){
audio.src = `audio/${file}.mp3`;
cover.src = `img/${file}.jpg`;
title.innerText = file;
}
// Initially load file details into DOM
loadFile(files[fileIndex]);
/**
* Play audio file
*/
function playFile(){
audioContainer.classList.add('play');
playBtn.querySelector('i.fas').classList.remove('fa-play');
playBtn.querySelector('i.fas').classList.add('fa-pause');
audio.play();
}
/**
* Pauses audio file
*/
function pauseFile(){
audioContainer.classList.remove('play');
playBtn.querySelector('i.fas').classList.add('fa-play');
playBtn.querySelector('i.fas').classList.remove('fa-pause');
audio.pause();
}
/**
* Player goes to the previous song in the array. If
* it is the first song then wrap to the last song.
*/
function prevFile(){
fileIndex--;
if(fileIndex < 0){
fileIndex = files.length - 1;
}
loadFile(files[fileIndex]);
playFile();
}
/**
* Player goes to the next song in the array. If it is the
* last song then wrap around to the first song.
*/
function nextFile(){
fileIndex++;
if(fileIndex > files.length -1){
fileIndex ^= fileIndex;
}
loadFile(files[fileIndex]);
playFile();
}
/**
* Update the progress bar
* @param {*} e the event
*/
function updateProgress(e) {
// Destructuring, extract variables from event source element
const { duration, currentTime } = e.srcElement;
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
}
/* Event Listeners */
playBtn.addEventListener('click', () => {
(audioContainer.classList.contains('play'))
? pauseFile() : playFile();
});
/**
* Update the progress bar
* @param {Event} e
*/
function updateProgress(e) {
const { duration, currentTime } = e.target;
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
}
/**
* Sets the current play time of the audio file to correspond to the
* progress bar's width that the user has specified.
* @param {Event} e progress bar event handler
*/
function setProgress(e){
const width = this.clientWidth;
const clickX = e.offsetX;
const duration = audio.duration;
audio.currentTime = (clickX / width) * duration;
}
// previous and next audio files
prevBtn.addEventListener('click', prevFile);
nextBtn.addEventListener('click', nextFile);
// audio ends
audio.addEventListener('ended', nextFile);
// Time/audio update
audio.addEventListener('timeupdate', updateProgress);
// Click on progress bar to set the time
progressContainer.addEventListener('click', setProgress);
/**
* Support for using 'Space Bar' to play and pause audio file.
* Added event listener to the document body instead.
*/
document.body.addEventListener('keydown', function(event){
if(event.code == 'Space'){
(audioContainer.classList.contains('play'))
? pauseFile() : playFile();
}
});