Pause video when changing slide in react js?

3k Views Asked by At

I have a simple slider using react slick slider one of my slides is a video using react player, Now I want if the user changes the slide to pause the playing video here is what I have so far.

Here is a live demo on code sandbox: pause video demo.

Player.js.

import React, { useState } from "react"
import ReactPlayer from "react-player"

function Player({ isPlaying, setIsPlaying }) {
  const handleisPlaying = () => {
    console.log("before playing?", isPlaying);
    setIsPlaying(false);
    console.log("after playing?", isPlaying);
  };
  return (
    <div>
      <ReactPlayer
        url="https://www.youtube.com/watch?v=5DjF0C3dybg"
        playing={isPlaying}
        volume={1}
        width="50vw"
        height="50vh"
      />
      <button
        type="button"
        className="btn btn-success"
        onClick={handleisPlaying}
      >
        pause
      </button>
    </div>
  );
}

export default Player;

And here is the slider code.

import React, { useRef, useState, useEffect } from "react";
import Slider from "react-slick";
import "slick-carousel/slick/slick.css";
import "slick-carousel/slick/slick-theme.css";
import Player from "./Player";

export default function APP() {
  const [isPlaying, setIsPlaying] = useState(false);
  const settings = {
    dots: true,
    beforeChange: () => {
      console.log("after change");
      setIsPlaying(false);
    }
  };

  return (
    <div className="container">
      <Slider {...settings}>
        <div>
          <img src="http://placekitten.com/g/400/200" />
        </div>
        <div>
          <img src="http://placekitten.com/g/400/200" />
        </div>
        <Player setIsPlaying={setIsPlaying} isPlaying={isPlaying} />
      </Slider>
    </div>
  );
}

react slick slider docs react slick docs.

What do I need to do to get this working?

2

There are 2 best solutions below

0
On BEST ANSWER

You are not setting the isPlaying to true when video starts.

Here is how you can do it.

 <ReactPlayer
        url="https://www.youtube.com/watch?v=5DjF0C3dybg"
        playing={isPlaying}
        onStart={() => setIsPlaying(true)}
        onPause={() => setIsPlaying(false)}
        onEnded={() => setIsPlaying(false)}
        volume={1}
        width="50vw"
        height="50vh"
      />

You can check it here as well. https://codesandbox.io/s/pause-de-video-forked-9788g?file=/src/Player.js

2
On

@saksh's answer won't work if you pause the video and resume again. I got the following from this comment by the maintainer of the react-player library: https://github.com/cookpete/react-player/issues/1152#issuecomment-767666288

 <ReactPlayer
        url="https://www.youtube.com/watch?v=5DjF0C3dybg"
        playing={isPlaying}
        onPlay={() => setIsPlaying(true)}
        onPause={() => setIsPlaying(false)}
        volume={1}
        width="50vw"
        height="50vh"
      />

See https://codesandbox.io/s/pause-de-video-forked-qb685?file=/src/Player.js