Is it possible to play the video simply by clicking it without using any React controls?

I am using html5 video player in my react app and want to escape the react controls and just start/stop videos by clicking on it. I found the solution to do this but it is in javascript and unable to transfer it in react. Can someone solve my problem?

1 Like

If you wish to bypass React controls and enable video playback with a simple click, you can do this by using React state and event handling.

Here is a detailed example,

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

const VideoPlayer = () => {
  const videoRef = useRef(null);
  const [isPlaying, setIsPlaying] = useState(false);

  const handleVideoClick = () => {
    const video = videoRef.current;

    if (video.paused) {
      video.play();
      setIsPlaying(true);
    } else {
      video.pause();
      setIsPlaying(false);
    }
  };

  return (
    <div>
      <video
        ref={videoRef}
        width="640"
        height="360"
        onClick={handleVideoClick}
      >
        <source src="your-video-url.mp4" type="video/mp4" />
        Your browser does not support the video tag.
      </video>
    </div>
  );
};

export default VideoPlayer;

In this example, the handleVideoClick function responds to a click on the video. It will check the video’s current state if it is playing or paused and toggles it as per the usage of isPlaying state. This may help you manage the play/pause status of the video.

Do not forget to replace “your-video-url.mp4” with your actual video URL. Feel free to adapt the component to suit your specific requirements.

Let me know if this clarifies things or if you have any further questions!

How can I add additional features, like volume control, to this video player?

Hey Nora!
To do that, it requires to include more functionality, including event handlers and more state variables or volume control. To manage volume changes, you could, for instance, use the video element’s volume attribute and design distinct routines.