-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
164 lines (137 loc) · 5.56 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const playerElement = document.querySelector('.player');
const video = document.getElementById('video');
const playAgainElement = document.querySelector('.play-again');
const progressRange = document.querySelector('.progress-range');
const progressBar = document.querySelector('.progress-bar');
const playBtn = document.getElementById('play-btn');
const volumeIcon = document.getElementById('volume-icon');
const volumeRange = document.querySelector('.volume-range');
const volumeBar = document.querySelector('.volume-bar');
const speedElement = document.querySelector('.player-speed');
const currentTimeElement = document.querySelector('.time-elapsed');
const durationElement = document.querySelector('.time-duration');
const fullscreenElement = document.querySelector('.fullscreen');
const fullscreenBtn = document.getElementById('fullscreen-btn');
// Play & Pause ----------------------------------- //
function playVideo() {
video.play();
playBtn.classList.replace('fa-play', 'fa-pause');
playBtn.setAttribute('title', 'Pause');
playAgainElement.hidden = true;
}
function showPlayIcon(event) {
playBtn.classList.replace('fa-pause', 'fa-play');
playBtn.setAttribute('title', 'Play');
if (event && event.type === 'ended') {
playAgainElement.hidden = false;
}
}
function togglePlay(event) {
if (video.paused) {
playVideo();
} else {
video.pause();
showPlayIcon();
}
}
// Progress Bar ---------------------------------- //
// Update DOM of progress bar as the video progresses
function updateProgress() {
const { duration, currentTime } = video;
// Update Progress Bar width in CSS
const progressPercent = (currentTime / duration) * 100;
progressBar.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 - Update Progress bar, change video's current time
function setProgress(event) {
const newtime = event.offsetX / progressRange.offsetWidth;
progressBar.style.width = `${newtime * 100}%`;
video.currentTime = newtime * video.duration;
}
// Volume Controls --------------------------- //
let volume = 1;
// Change volume icon based on range
function changeVolumeIcon() {
volumeIcon.className = '';
if (video.volume > 0.7) {
volumeIcon.classList.add('fas', 'fa-volume-up');
} else if (video.volume < 0.7 && video.volume > 0) {
volumeIcon.classList.add('fas', 'fa-volume-down');
} else if (video.volume === 0) {
volumeIcon.classList.add('fas', 'fa-volume-mute');
}
}
// Click anywhere on the volume Bar - Update Volume bar, change video's volume, set global volume variable, change volume icon
function changeVolume(event) {
let newVolume = event.offsetX / volumeRange.offsetWidth;
newVolume = (newVolume < 0.1) ? 0 : newVolume;
newVolume = (newVolume > 0.9) ? 1 : newVolume;
volumeBar.style.width = `${newVolume * 100}%`;
video.volume = newVolume;
volume = newVolume;
changeVolumeIcon();
}
// Mute / Unmute
function toggleMute() {
if (video.volume) {
video.volume = 0;
volumeIcon.setAttribute('title', 'Unmute');
volumeBar.style.width = 0;
} else {
video.volume = volume;
volumeIcon.setAttribute('title', 'Mute');
volumeBar.style.width = `${volume * 100}%`;;
}
changeVolumeIcon();
}
// Change Playback Speed -------------------- //
function changePlaybackRate() {
video.playbackRate = speedElement.value;
}
// Fullscreen ------------------------------- //
let fullscreen = false;
/* View in fullscreen */
function openFullscreen(elem) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.webkitRequestFullscreen) { /* Safari */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE11 */
elem.msRequestFullscreen();
}
fullscreenBtn.classList.replace('fa-expand', 'fa-compress');
video.classList.add('video-fullscreen');
}
/* Close fullscreen */
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) { /* Safari */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) { /* IE11 */
document.msExitFullscreen();
}
fullscreenBtn.classList.replace('fa-compress', 'fa-expand');
video.classList.remove('video-fullscreen');
}
function toggleFullScreen() {
fullscreen ? closeFullscreen() : openFullscreen(playerElement);
fullscreen = !fullscreen;
}
// Event Listeners ------------------------------- //
playBtn.addEventListener('click', togglePlay);
video.addEventListener('click', togglePlay);
video.addEventListener('ended', showPlayIcon);
video.addEventListener('timeupdate', updateProgress);
video.addEventListener('canplay', updateProgress);
playAgainElement.addEventListener('click', playVideo);
progressRange.addEventListener('click', setProgress);
volumeRange.addEventListener('click', changeVolume);
volumeIcon.addEventListener('click', toggleMute);
speedElement.addEventListener('change', changePlaybackRate);
fullscreenElement.addEventListener('click', toggleFullScreen);