-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
160 lines (138 loc) · 5.24 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
const videoPlayer = {}
videoPlayer.player = document.querySelector(".player");
videoPlayer.video = document.querySelector("video");
videoPlayer.progressRange = document.querySelector(".progress-range");
videoPlayer.progressBar = document.querySelector(".progress-bar");
videoPlayer.playBtn = document.getElementById("play-btn");
videoPlayer.volumeIcon = document.getElementById("volume-icon");
videoPlayer.volumeRange = document.querySelector(".volume-range");
videoPlayer.volumeBar = document.querySelector(".volume-bar");
videoPlayer.currrentTime = document.querySelector(".time-elapsed");
videoPlayer.duration = document.querySelector(".time-duration");
videoPlayer.speed = document.querySelector(".player-speed");
videoPlayer.fullscreenBtn = document.querySelector(".fullscreen");
// Play & Pause ----------------------------------- //
function showPlayIcon() {
videoPlayer.playBtn.classList.replace("fa-pause", "fa-play");
videoPlayer.playBtn.setAttribute("title", "Play");
}
function togglePlay() {
if (videoPlayer.video.paused) {
videoPlayer.video.play();
videoPlayer.playBtn.classList.replace("fa-play", "fa-pause");
videoPlayer.playBtn.setAttribute("title", "Pause");
} else {
videoPlayer.video.pause();
showPlayIcon();
}
}
// On Video End, show Play Button Icon
videoPlayer.video.addEventListener("ended", showPlayIcon);
// Progress Bar ---------------------------------- //
// Calculate display time format
function displayTime(time) {
const minutes = Math.floor(time / 60);
let seconds = Math.floor(time % 60);
seconds = seconds > 9 ? seconds : `0${seconds}`;
return `${minutes}:${seconds}`;
}
// Update Progress Bar as video plays
function updateProgress() {
videoPlayer.progressBar.style.width = `${(videoPlayer.video.currentTime / videoPlayer.video.duration) * 100}%`;
videoPlayer.currrentTime.textContent = `${displayTime(videoPlayer.video.currentTime)} /`;
videoPlayer.duration.textContent = `${displayTime(videoPlayer.video.duration)}`;
}
// Click to seek within the video
function setProgress(e) {
const newTime = e.offsetX / videoPlayer.progressRange.offsetWidth;
videoPlayer.progressBar.style.width = `${newTime * 100}%`;
videoPlayer.video.currentTime = newTime * videoPlayer.video.duration;
}
// Volume Controls --------------------------- //
let lastVolume = 1;
// Volume Bar
function changeVolume(e) {
let volume = e.offsetX / videoPlayer.volumeRange.offsetWidth;
// Rounding Volume up or down
if (volume < 0.1) {
volume = 0;
}
if (volume > 0.9) {
volume = 1;
}
videoPlayer.volumeBar.style.width = `${volume * 100}%`;
videoPlayer.video.volume = volume;
// Change Icon depending on Volume
videoPlayer.volumeIcon.className = "";
if (volume > 0.5) {
videoPlayer.volumeIcon.classList.add("fas", "fa-volume-up");
} else if (volume < 0.5 && volume > 0) {
videoPlayer.volumeIcon.classList.add("fas", "fa-volume-down");
} else if (volume === 0) {
videoPlayer.volumeIcon.classList.add("fas", "fa-volume-off");
}
lastVolume = volume;
}
// Mute/Unmute
function toggleMute() {
videoPlayer.volumeIcon.className = "";
if (videoPlayer.video.volume) {
lastVolume = videoPlayer.video.volume;
videoPlayer.video.volume = 0;
videoPlayer.volumeBar.style.width = 0;
videoPlayer.volumeIcon.classList.add("fas", "fa-volume-mute");
videoPlayer.volumeIcon.setAttribute("title", "Unmute");
} else {
videoPlayer.video.volume = lastVolume;
videoPlayer.volumeBar.style.width = `${lastVolume * 100}%`;
videoPlayer.volumeIcon.classList.add("fas", "fa-volume-up");
videoPlayer.volumeIcon.setAttribute("title", "Mute");
}
}
// Change Playback Speed -------------------- //
function changeSpeed() {
videoPlayer.video.playbackRate = videoPlayer.speed.value;
}
// Fullscreen ------------------------------- //
/* 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();
}
videoPlayer.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();
}
videoPlayer.video.classList.remove("video-fullscreen");
}
let fullscreen = false;
// Toggle Fullscreen
function toggleFullscreen() {
!fullscreen ? openFullscreen(videoPlayer.player) : closeFullscreen();
fullscreen = !fullscreen;
}
// Event Listeners
videoPlayer.playBtn.addEventListener("click", togglePlay);
videoPlayer.video.addEventListener("click", togglePlay);
videoPlayer.video.addEventListener("timeupdate", updateProgress);
videoPlayer.video.addEventListener("canplay", updateProgress);
videoPlayer.progressRange.addEventListener("click", setProgress);
videoPlayer.volumeRange.addEventListener("click", changeVolume);
videoPlayer.volumeIcon.addEventListener("click", toggleMute);
videoPlayer.speed.addEventListener("change", changeSpeed);
videoPlayer.fullscreenBtn.addEventListener("click", toggleFullscreen);