videoPlayer.js
3.92 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 videoContainer = document.getElementById("jsVideoPlayer");
const videoPlayer = document.querySelector("#jsVideoPlayer video");
const playBtn = document.getElementById("jsPlayButton");
const volumeBtn = document.getElementById("jsVolumeBtn");
const fullScrnBtn = document.getElementById("jsFullScreen");
const currentTime = document.getElementById("currentTime");
const totalTime = document.getElementById("totalTime");
const volumeRange = document.getElementById("jsVolume");
// video element MDN
// using MDN (Mozila Developer Networks)
function handlePlayClick() {
if (videoPlayer.paused) {
videoPlayer.play();
playBtn.innerHTML = '<i class="fas fa-pause"></i>';
} else {
videoPlayer.pause();
playBtn.innerHTML = '<i class="fas fa-play"></i>';
}
}
function handleVolumeClick() {
if (videoPlayer.muted) {
videoPlayer.muted = false;
volumeBtn.innerHTML = '<i class="fas fa-volume-up"></i>';
volumeRange.value = videoPlayer.volume;
} else {
volumeRange.value = 0;
videoPlayer.muted = true;
volumeBtn.innerHTML = '<i class="fas fa-volume-mute"></i>';
}
}
function exitFullScreen() {
fullScrnBtn.innerHTML = '<i class="fas fa-expand"></i>';
fullScrnBtn.removeEventListener("click", exitFullScreen);
fullScrnBtn.addEventListener("click", goFullScreen);
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.mxExitFullscreen) {
document.mxExitFullscreen();
}
}
function goFullScreen() {
if (videoContainer.requestFullscreen) {
videoContainer.requestFullscreen();
} else if (videoContainer.mozRequestFullScreen) {
videoContainer.mozRequestFullScreen();
} else if (videoContainer.webkitRequestFullscreen) {
videoContainer.webkitRequestFullscreen();
} else if (videoContainer.msRequestFullscreen) {
videoContainer.msRequestFullscreen();
}
fullScrnBtn.innerHTML = '<i class="fas fa-compress"></i>';
fullScrnBtn.removeEventListener("click", goFullScreen);
fullScrnBtn.addEventListener("click", exitFullScreen);
}
const formatDate = (seconds) => {
const secondsNumber = parseInt(seconds, 10);
let hours = Math.floor(secondsNumber / 3600);
let minutes = Math.floor((secondsNumber - hours * 3600) / 60);
let totalSeconds = secondsNumber - hours * 3600 - minutes * 60;
if (hours < 10) {
hours = `0${hours}`;
}
if (minutes < 10) {
minutes = `0${minutes}`;
}
if (totalSeconds < 10) {
totalSeconds = `0${totalSeconds}`;
}
return `${hours}:${minutes}:${totalSeconds}`;
};
function getCurrentTime() {
currentTime.innerHTML = formatDate(Math.floor(videoPlayer.currentTime));
}
function setTotalTime() {
const totalTimeString = formatDate(videoPlayer.duration);
totalTime.innerHTML = totalTimeString;
setInterval(getCurrentTime, 1000);
}
// 영상이 끝나면 pause하고 다시 앞으로 돌릴 수 있게 하고 싶다...
function handleEnded() {
videoPlayer.currentTime = 0;
playBtn.innerHTML = '<i class="fas fa-play"></i>';
}
function handleDrag(event) {
const {
target: { value },
} = event;
videoPlayer.volume = value;
if (value >= 0.6) {
volumeBtn.innerHTML = '<i class="fas fa-volume-up"></i>';
} else if (value >= 0.2) {
volumeBtn.innerHTML = '<i class="fas fa-volume-down"></i>';
} else {
volumeBtn.innerHTML = '<i class="fas fa-volume-mute"></i>';
}
}
function init() {
videoPlayer.volume = 0.5;
playBtn.addEventListener("click", handlePlayClick);
volumeBtn.addEventListener("click", handleVolumeClick);
fullScrnBtn.addEventListener("click", goFullScreen);
videoPlayer.addEventListener("loadedmetadata", setTotalTime);
videoPlayer.addEventListener("ended", handleEnded);
volumeRange.addEventListener("input", handleDrag);
}
if (videoContainer) {
init();
} // 안 해주면 다른 페이지에서도 js 읽히므로..