-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (106 loc) · 3.63 KB
/
Copy pathscript.js
File metadata and controls
122 lines (106 loc) · 3.63 KB
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
const image = document.getElementById('album-art');
const title = document.getElementById('title');
const artist = document.getElementById('artist');
const music = document.getElementById('audio');
const progressContainer = document.getElementById('progress-container');
const progress = document.getElementById('progress');
const currentTimeElement = document.getElementById('current-time');
const durationElement = document.getElementById('duration');
const prevBtn = document.getElementById('prev');
const playBtn = document.getElementById('play');
const nextBtn = document.getElementById('next');
// Music
const songs = [
{
name: 'Seven-Nation-Army',
displayName: 'Seven Nation Army',
artist: 'The White Stripes',
},
{
name: 'Ziddi-Dil',
displayName: 'Ziddi Dil',
artist: 'Vishal Dadlani',
},
{
name: 'Zinda',
displayName: 'Zinda',
artist: 'Siddharth Mahadevan',
},
{
name: 'Hey-There-Delilah',
displayName: 'Hey There Delilah',
artist: `Plain White T's`,
},
{
name: 'Woh-Ladki',
displayName: 'Woh Ladki',
artist: 'Arijit Singh',
},
];
let isPlaying = false;
let currentSongIndex = 0;
// Update DOM
function loadSong(song) {
title.textContent = song.displayName;
artist.textContent = song.artist;
music.src = `music/${song.name}.mp3`;
image.src = `img/${song.name}.jpg`;
}
function playSong() {
isPlaying = true;
music.play();
playBtn.classList.replace('fa-play', 'fa-pause');
playBtn.setAttribute('title', 'Pause');
}
function pauseSong() {
isPlaying = false;
music.pause();
playBtn.classList.replace('fa-pause', 'fa-play');
playBtn.setAttribute('title', 'Play');
}
function prevSong() {
currentSongIndex--;
if (currentSongIndex < 0) {
currentSongIndex = songs.length - 1;
}
loadSong(songs[currentSongIndex]);
playSong();
}
function nextSong() {
currentSongIndex++;
if (currentSongIndex > songs.length - 1) {
currentSongIndex = 0;
}
loadSong(songs[currentSongIndex]);
playSong();
}
// Update DOM of progress bar as the song progresses
function updateProgressBar(event) {
if (isPlaying) {
const { duration, currentTime } = event.srcElement;
// Update Progress Bar width in CSS
const progressPercent = (currentTime / duration) * 100;
progress.style.width = `${progressPercent}%`;
// Get duration and currentTime in minutes to 2 decimal places, convert to String, replace . with :, check for NaN, and update DOM
const durationMinutes = (duration / 60).toFixed(2);
const currentTimeMinutes = (currentTime / 60).toFixed(2);
durationElement.textContent = isNaN(durationMinutes) ? '00:00' : durationMinutes.toString().replace('.', ':');
currentTimeElement.textContent = currentTimeMinutes.toString().replace('.', ':');
}
}
// Click anywhere on the progress Bar to play from there
function setProgressBar(event) {
const clientWidth = this.clientWidth;
const { offsetX } = event;
const { duration } = music;
music.currentTime = (offsetX / clientWidth) * duration; // set the current time of the audio element (in seconds)
}
// Event Listeners
playBtn.addEventListener('click', () => (isPlaying ? pauseSong() : playSong()));
prevBtn.addEventListener('click', prevSong);
nextBtn.addEventListener('click', nextSong);
music.addEventListener('timeupdate', updateProgressBar);
music.addEventListener('ended', nextSong);
progressContainer.addEventListener('click', setProgressBar);
// On load - select first song
loadSong(songs[currentSongIndex]);