Home > Enterprise >  How can I start a video at specific timestamp in React?
How can I start a video at specific timestamp in React?

Time:11-29

Here is my component and I want to automatically play it from a certain time like 00:07:12,600, not from the start.

import style from './Hero.module.css';
import Image from 'next/image';
import ReactPlayer from 'react-player';
import { useState } from 'react';

export default function Index() {
  const [isPlaying, setIsPlaying] = useState(true);

  return (
    <div className={style.hero_container}>
      {/* <Image src="/images/hero/hero1.jpg" alt="Logo" height={400} width={700} /> */}

      <ReactPlayer
        url="/videos/Dexter.S01E03.1080p.5.1Ch.BluRay.ReEnc-DeeJayAhmed.mkv"
        playing={isPlaying}
        width="100%"
        height="100%"
        controls={true}
      />
    </div>
  );
}

CodePudding user response:

Use the onReady event together with the seekTo method.

Something like this

const playerRef = React.useRef();

const onReady = React.useCallback(() => {
  const timeToStart = (7 * 60)   12.6;
  playerRef.current.seekTo(timeToStart, 'seconds');
}, [playerRef.current]);

<ReactPlayer
   ref={playerRef}
   url="/videos/Dexter.S01E03.1080p.5.1Ch.BluRay.ReEnc-DeeJayAhmed.mkv"
   playing={isPlaying}
   width="100%"
   height="100%"
   controls={true}
   onReady={onReady}
/>

Update

Looks like onReady is fired after each seek event so we need some extra logic.

  const [isPlaying, setIsPlaying] = React.useState(true);
  const [isReady, setIsReady] = React.useState(false);
  const playerRef = React.useRef();

  const onReady = React.useCallback(() => {
    if (!isReady) {
      const timeToStart = (7 * 60)   12.6;
      playerRef.current.seekTo(timeToStart, "seconds");
      setIsReady(true);
    }
  }, [isReady]);

CodePudding user response:

You can use seekTo function to start the video on a specific time.

WORKING DEMO

Edit #SO-material-onclick-edit

class Player extends React.Component {
  render () {
    return (
        <div className='player-wrapper'>
        <ReactPlayer
          ref={p => { this.p = p }}
          url='//s3.envoy.rocks/bothrs/goud-design-sprint/goud/LhgEcS_GOUD PROTOTYPE SHOWCASE.mp4'
          className='react-player'
          playing
          controls
          width='100%'
          height='100%'
        />
        <button onClick={() => this.p.seekTo(0.9999999)}>Seek to end</button>
        <button onClick={() => this.p.seekTo(0.999)}>Seek to end (works in Safari)</button>
        <button onClick={() => {
    console.log(this.p.getDuration());this.p.seekTo(this.p.getDuration())}}>Seek to end (with getDuration())</button>
        <button onClick={() => this.p.seekTo(12.7)}>Seek to 12.7</button>
        <button onClick={() => this.p.seekTo(42.65)}>Seek to 42.65</button>
      </div>
    )
  }
}

ReactDOM.render(
  <Player />,
  document.getElementById('container')
);

UPDATE If you want to start the video on specific time then I think useEffect with empty dependency would be best approach.

useEffect(() => {
   this.p.seekTo(12.7)
}, [])
  • Related