๐ช logic
play
๊ธฐ๋ฅ๊ณผ stop
& pause
๊ธฐ๋ฅ ๊ตฌํ์ด ํต์ฌ
- ๋น๋์ค ํ๋ฉด ๊ณผ play button ์ ๋๋ฅด๋ฉด
Play
๊ธฐ๋ฅ ์คํ
play
๊ธฐ๋ฅ์ด ์คํ ์ค ์ผ๋ โก play button
์ pause button
์ํ
โ (pause
์ํ์ผ ๋ โก play button
์ํ)
stop button
์ ๋๋ฅด๋ฉด Stop ๊ธฐ๋ฅ ์คํ ๋ฐ range bar ์ด๊ธฐํ
- ํ์ฌ ์ฌ์ ์ํ์ ๋ฐ๋ผ range bar ํ์ฑํ && time stamp(๊ฒฝ๊ณผ์๊ฐ) ํ์ฑํ
๐ธ html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Video player practice</title>
<link
href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
rel="stylesheet"
integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN"
crossorigin="anonymous"
/>
<link href="index.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<h1>My video player</h1>
<video src="bear.mp4" poster="poster.png"></video>
<div class="control">
<button class="btn" id="play">
<i class="fa fa-play fa-2x"></i>
</button>
<button class="btn" id="stop">
<i class="fa fa-stop fa-2x"></i>
</button>
<input type="range" class="progress" min="0" max="100" step="0.1" value="0" />
<span id="timestamp">00:00</span>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
๐ธ css
@import url("reset.css");
body {
background-color: linear-gradient(to right, orange, green);
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
}
h1 {
margin-bottom: 30px;
font-size: 40px;
font-weight: 900;
}
video {
width: 50%;
height: 50%;
background-color: rgb(37, 37, 37);
cursor: pointer;
}
.control {
display: flex;
justify-content: space-between;
width: 50%;
background-color: rgb(106, 4, 117);
cursor: pointer;
}
.btn {
margin: 3px;
padding: 5px;
color: #fafafa;
background-color: rgb(171, 109, 230);
font-size: 10px;
border-radius: 5px;
}
.progress {
width: 100%;
cursor: pointer;
}
#timestamp {
padding: 10px;
color: #fafafa;
font-weight: 900;
}
๐ธ javascript
"use strict";
const playBtn = document.getElementById("play");
const stopBtn = document.getElementById("stop");
const video = document.querySelector("video");
const progress = document.querySelector(".progress");
const timestamp = document.getElementById("timestamp");
function toggleVideoStatus(e) {
if (video.paused) {
video.play();
} else {
video.pause();
}
}
video.addEventListener("click", toggleVideoStatus);
playBtn.addEventListener("click", toggleVideoStatus);
function updateIcon(e) {
if (video.paused) {
play.innerHTML = '<i class="fa fa-play fa-2x"></i>';
} else {
play.innerHTML = '<i class="fa fa-pause fa-2x"></i>';
}
}
โ default ๋ฒํผ์ 'play' ๋ฒํผ์ผ๋ก ๋์ด์๊ธฐ ๋๋ฌธ์, if ์กฐ๊ฑด์ "pause ๋์์ ๋, ์ผ์์ ์ง ์์ด์ฝ ๋ฃ์ด์ฃผ๊ธฐ"๋ฅผ ํด์ผํ๋ค.
โ ๋ฐ๋์ ๊ฒฝ์ฐ(video.played)๋ก ํ๋ฉด ๋น๋์ค๊ฐ ๋ฉ์ถฐ๋ ๊ณ์ ์ผ์์ ์ง ์์ด์ฝ์ด ๋ ์๋๋ค
playBtn.addEventListener("click", updateIcon);
video.addEventListener("click", updateIcon);
function stopVideo(e) {
video.currentTime = 0;
video.pause();
}
stopBtn.addEventListener("click", stopVideo);
video.addEventListener("timeupdate", (e) => {
progress.value = (video.currentTime / video.duration) * 100;
let mins = Math.floor(video.currentTime / 60);
if (mins < 10) {
mins = "0" + String(mins);
}
let secs = Math.floor(video.currentTime % 60);
if (secs < 10) {
secs = "0" + String(secs);
}
timestamp.innerHTML = `${mins}:${secs}`;
});
timestamp.addEventListener("change", (e) => {
video.currentTime = (+timestamp.value * video.duration) / 100;
});