๐ŸŽจ video-player

kirin.logยท2021๋…„ 3์›” 4์ผ
1
post-custom-banner

๐Ÿช 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>
       
        <!-- video dafault poster -->
        <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 ์ž…๋ ฅ ํ•„๋“œ -->
                <input type="range" class="progress" min="0" max="100" step="0.1" value="0" />
                          <!-- step: <input> ์š”์†Œ์— ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž๋“ค ์‚ฌ์ด์˜ ๊ฐ„๊ฒฉ์„ ๋ช…์‹œ -->
                          <!-- value : <input> ์š”์†Œ์˜ ์ดˆ๊นƒ๊ฐ’์„ ๋ช…์‹œ -->

                <!-- ๊ฒฝ๊ณผ์‹œ๊ฐ„ ํ‘œ์‹œ -->
                <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";

// (1) play & stop ๊ธฐ๋Šฅ ํ•จ์ˆ˜ ์ƒ์„ฑ
// (2) ๋น„๋””์˜ค์ฐฝ ๋ˆ„๋ฅด๋ฉด stop (ํ•œ๋ฒˆ ๋” ๋ˆ„๋ฅด๋ฉด play) >> toggle ๊ธฐ๋Šฅ
// (3) play ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์žฌ์ƒ (ํ•œ๋ฒˆ ๋” ๋ˆ„๋ฅด๋ฉด ์ผ์‹œ์ •์ง€)
// (4) stop ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด range ์ฒ˜์Œ์œผ๋กœ
// (5) ์žฌ์ƒ range์— ๋”ฐ๋ผ ๊ฒฝ๊ณผ์‹œ๊ฐ„ upload

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");

// play & stop ๊ธฐ๋Šฅ
function toggleVideoStatus(e) {
    // console.log(e.target);
    if (video.paused) {
        video.play();   // play() โžก ์˜์ƒ ์žฌ์ƒํ•˜๋Š” ๋ฉ”์„œ๋“œ
    } else {
        video.pause();  // pause() โžก ์˜์ƒ ์ค‘์ง€ํ•˜๋Š” ๋ฉ”์„œ๋“œ
    }
}
video.addEventListener("click", toggleVideoStatus);
playBtn.addEventListener("click", toggleVideoStatus);

// play ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์ผ์‹œ์ •์ง€ ์•„์ด์ฝ˜ ์ƒ์„ฑ
function updateIcon(e) {
    // console.log(e.target);
    if (video.paused) {      // 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);
// ์•„๋ž˜์™€ ๊ฐ™์ด eventListner๋ฅผ ๊ฑธ์–ด๋„ ๋™์ผํ•จ
// video.addEventListener("play", updateIcon);
// video.addEventListener("pause", updateIcon);

// stop ๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด range ์ฒ˜์Œ์œผ๋กœ
function stopVideo(e) {
    // console.log(e.target);
    video.currentTime = 0;
    // ๋น„๋””์˜ค ํ˜„์žฌ ์‹œ๊ฐ„์„ 0์ดˆ๋กœ ๋ณ€๊ฒฝ
    video.pause();
}
stopBtn.addEventListener("click", stopVideo);

//range ์„ค์ •
video.addEventListener("timeupdate", (e) => {
    // "timeupdate" : currentTime์ด ๋ณ€๊ฒฝ๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ
    //console.log(e.target);
    progress.value = (video.currentTime / video.duration) * 100;
    // ํ˜„์žฌ ์žฌ์ƒ์ค‘์ธ ์‹œ๊ฐ„  // ๋ฏธ๋””์–ด์˜ ์žฌ์ƒ ๊ธธ์ด

    let mins = Math.floor(video.currentTime / 60); // ํ˜„์žฌ ์‹œ๊ฐ„์„ 60์œผ๋กœ ๋‚˜๋ˆˆ ๋ชซ
    if (mins < 10) {
        mins = "0" + String(mins);
    }

    let secs = Math.floor(video.currentTime % 60); // ํ˜„์žฌ ์‹œ๊ฐ„์„ 60์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€
    if (secs < 10) {
        secs = "0" + String(secs);
    }
    timestamp.innerHTML = `${mins}:${secs}`;
});

// range bar ์‹œ๊ฐ„ ์„ค์ •
timestamp.addEventListener("change", (e) => {
    // console.log(e.target);
    video.currentTime = (+timestamp.value * video.duration) / 100;
    // (์ง„ํ–‰์‹œ๊ฐ„ ๋”ํ•œ๊ฐ’ X ์ „์ฒด ์žฌ์ƒ๊ธธ์ด) / 100
}); 
profile
boma91@gmail.com
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€