最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Update the value of the Progress tag while video plays - Stack Overflow

programmeradmin6浏览0评论

I'm trying to create a custom video progress bar using the HTML progress tag. The video player is a React ponent. All I want to do is update the value of the progress tag as the video continues to play and have it show a green colour as it starts to fill. I have tried a couple other methods like filling the SVG path but the progress tag seems to be the best option. I have created a function called progressLoop that is supposed to update the value of the progress bar but being a React beginner, I'm not sure what the right place is for calling it.

React:

import React, {useRef} from 'react'



const VideoSection = () => {
  const videoRef = useRef()

  const handlePlayVideo = () => videoRef.current.play()

   const progressLoop = () => {
    setInterval(function () {
     document.getElementById("progress").value = Math.round(
       (video.currentTime / video.duration) * 100
      );
    });
   };

  return (
    <div>
       <button onClick={handlePlayVideo}>
        Play video
      </button>
      
      <video src='./video.mp4' ref={videoRef}/>
      
      <progress
       id="progress"
       max="100"
       value="0"
        >
       Progress
      </progress>

      
    </div>
  )
}

export default VideoSection

CSS:

progress[value] {
  appearance: none;
  border: none;
  color: red;
}
progress[value]::-webkit-progress-value {
  background-color: green;
}

I'm trying to create a custom video progress bar using the HTML progress tag. The video player is a React ponent. All I want to do is update the value of the progress tag as the video continues to play and have it show a green colour as it starts to fill. I have tried a couple other methods like filling the SVG path but the progress tag seems to be the best option. I have created a function called progressLoop that is supposed to update the value of the progress bar but being a React beginner, I'm not sure what the right place is for calling it.

React:

import React, {useRef} from 'react'



const VideoSection = () => {
  const videoRef = useRef()

  const handlePlayVideo = () => videoRef.current.play()

   const progressLoop = () => {
    setInterval(function () {
     document.getElementById("progress").value = Math.round(
       (video.currentTime / video.duration) * 100
      );
    });
   };

  return (
    <div>
       <button onClick={handlePlayVideo}>
        Play video
      </button>
      
      <video src='./video.mp4' ref={videoRef}/>
      
      <progress
       id="progress"
       max="100"
       value="0"
        >
       Progress
      </progress>

      
    </div>
  )
}

export default VideoSection

CSS:

progress[value] {
  appearance: none;
  border: none;
  color: red;
}
progress[value]::-webkit-progress-value {
  background-color: green;
}
Share Improve this question asked Jul 20, 2021 at 15:56 H.shaH.sha 612 silver badges4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Instead of adding interval, you can add 'onProgress' event handler.

import React, { useRef, useState } from "react";

import videoFile from "./Video.mov";

const VideoSection = () => {
  const videoRef = useRef();
  const [progress, setProgress] = useState(0);
  const handlePlayVideo = () => videoRef.current.play();

  const handleProgress = (e) => {
    if (isNaN(e.target.duration))   // duration is NotaNumber at Beginning.
      return;
    setProgress((e.target.currentTime / e.target.duration) * 100);
  };
  return (
    <div>
      <button onClick={handlePlayVideo}>Play video</button>

      <video
        controls
        onProgress={handleProgress}
        src={videoFile}
        width="400"
        height="500"
        ref={videoRef}
      />

      <progress id="progress" max="100" value={progress}>
        Progress
      </progress>
    </div>
  );
};

export default VideoSection;

You can use the onTimeUpdate event handler. See documentation

<video
   onTimeUpdate={(e) => {
      setProgress(e.target.currentTime / e.target.duration);
   }}
/>

The onProgress event handler is not correct because it is for when video/audio data is being downloaded. See documentation

发布评论

评论列表(0)

  1. 暂无评论