Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant detect when video has ended in nextjs #1091

Open
willzfrank opened this issue Jul 20, 2023 · 0 comments
Open

Cant detect when video has ended in nextjs #1091

willzfrank opened this issue Jul 20, 2023 · 0 comments

Comments

@willzfrank
Copy link

willzfrank commented Jul 20, 2023

I am attempting to detect when a video ends and trigger a specific behavior using a prop. However, the code I have doesn't seem to be working. Even the console log I added inside the function is not appearing. Can someone help me understand why?

Here's the code I am using:

useEffect(() => {
if (videoRef.current) {
console.log('Starting');

videoRef.current.restart();
videoRef.current.on('ended', function (event: any) {
  props.onVideoEnded && props.onVideoEnded();
  console.log('Ending');
});

}
}, []);


and here is the full code import React, { useRef, useEffect, useState } from 'react';
import styles from './videoplayer.module.css';
import { motion } from 'framer-motion';
import Plyr from 'plyr-react';
import 'plyr-react/plyr.css';

interface VideoPlayerProps {
  src: string | SourceInfo[];
  title?: string;
  description?: string;
  posterUrl?: string;
  subtitleUrl?: string;
  className?: string;
  onVideoEnded?: () => void;
}

interface SourceInfo {
  src: string;
  type: string;
}

function VideoCoursePlayer(props: VideoPlayerProps): JSX.Element {
  const [videoSrc, setVideoSrc] = useState<SourceInfo[]>([]);
  const videoRef = useRef<any>(null);

  useEffect(() => {
    if (props.src) {
      setVideoSrc(
        Array.isArray(props.src) ? props.src : [{ src: props.src, type: '' }]
      );
    }
  }, [props.src]);

  useEffect(() => {
    if (videoRef.current) {
      console.log('starting');

      videoRef.current.restart();
      videoRef.current.on('ended', function (event: any) {
        props.onVideoEnded && props.onVideoEnded();
        console.log('ending');
      });
    }
  }, []);

  return (
    <div className="w-full">
      <motion.div>
        <h6 className="capitalize inter">{props.title}</h6>
        <div>
          <div>
            <Plyr
              options={{
                ratio: '16:9',
              }}
              source={{
                type: 'video',
                sources: videoSrc,
              }}
              ref={videoRef}
              onEnded={() => {
                props.onVideoEnded && props.onVideoEnded();
                console.log('ending');
              }}
            />
          </div>
        </div>
      </motion.div>
    </div>
  );
}

export default VideoCoursePlayer;


I would appreciate any assistance in figuring out why the video ending detection is not functioning as expected."

@willzfrank willzfrank changed the title Cant know when video has ended Cant detect when video has ended in nextjs Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant