-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
145 lines (116 loc) · 3.83 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
const video = document.getElementById("video");
const play = document.getElementById("play");
const timestamp = document.getElementById("timestamp");
const totalTimestamp = document.getElementById("total-timestamp");
const progress = document.querySelector(".progress");
const progressBar = document.querySelector(".progress__filled");
const speedLevels = document.querySelectorAll(".speed-level");
const volumeLevels = document.querySelectorAll(".volume-level");
const speedControls = document.querySelector(".speed-controls");
const volumeControls = document.querySelector(".volume-controls");
const fullScreenBtn = document.querySelector(".fullscreen-btn");
const videoContainer = document.querySelector(".video-container");
const rewind = document.querySelector(".rewind-button");
console.log(rewind);
rewind.addEventListener("click", function(e) {
const skipAttribute = e.target.getAttribute("data-skip");
video.currentTime -= skipAttribute;
});
// Remove currentSpeed class from all list-items
function removeCurrentSpeed() {
for (let i = 0; i < speedLevels.length; i++) {
speedLevels[i].classList.remove("currentSpeed");
}
}
// Remove currentVolume class from all list-items
function removeCurrentVolume() {
for (let i = 0; i < volumeLevels.length; i++) {
volumeLevels[i].classList.remove("currentVolume");
}
}
// Play & Pause video
function toggleVideoStatus() {
if (video.paused) {
video.play();
} else {
video.pause();
}
}
// Getting total duration of video
function totalDuration() {
let mins = Math.floor(video.duration / 60);
if (mins < 10) {
mins = "0" + String(mins);
}
// Get seconds
let secs = Math.floor(video.duration % 60);
if (secs < 10) {
secs = "0" + String(secs);
}
totalTimestamp.innerText = `${mins}:${secs}`;
}
// update play/pause icon
function updatePlayIcon() {
if (video.paused) {
play.innerHTML = '<ion-icon name="play"></ion-icon>';
} else {
play.innerHTML = '<ion-icon name="pause"></ion-icon>';
}
}
// Stop video
function stopVideo() {
console.log("stopvideo");
video.currentTime = 0;
video.pause();
}
// update progress & timestamp
function updateProgress() {
// console.log("updateprogress");
value = (video.currentTime / video.duration) * 100;
progressBar.style.flexBasis = `${value}%`;
// Get minutes
let mins = Math.floor(video.currentTime / 60);
if (mins < 10) {
mins = "0" + String(mins);
}
// Get seconds
let secs = Math.floor(video.currentTime % 60);
if (secs < 10) {
secs = "0" + String(secs);
}
timestamp.innerText = `${mins}:${secs}`;
}
// set video time to progress
function setVideoProgress(e) {
const progressUpdate = (e.offsetX / progress.offsetWidth) * video.duration;
video.currentTime = progressUpdate;
}
// console.log(video.duration);
// Event Listeners
video.addEventListener("click", toggleVideoStatus);
video.addEventListener("pause", updatePlayIcon);
video.addEventListener("play", updatePlayIcon);
video.addEventListener("timeupdate", updateProgress);
play.addEventListener("click", toggleVideoStatus);
// stop.addEventListener("click", stopVideo);
progress.addEventListener("click", setVideoProgress);
window.addEventListener("load", function() {
totalDuration();
});
fullScreenBtn.addEventListener("click", function() {
videoContainer.requestFullscreen();
});
speedControls.addEventListener("click", function(e) {
removeCurrentSpeed();
e.target.classList.add("currentSpeed");
const speedAttribute = event.target.getAttribute("dataSpeed");
video.playbackRate = speedAttribute;
});
volumeControls.addEventListener("click", function(e) {
removeCurrentVolume();
e.target.classList.add("currentVolume");
const volumeAttribute = event.target.getAttribute("volumeSpeed");
console.log(volumeAttribute);
video.volume = volumeAttribute;
});
progress.addEventListener("click", setVideoProgress(e));