Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect when video finished playing react?

Tags:

reactjs

Since React uses lifecycle methods and the virtual dom how can I detect when my video has finished playing. I found another question like this on stackoverflow that suggested to use componentWillUnmount but there's no point at which I remove the video from the dom, so componentWillUnmount won't get fired...

How can I detect when video finished playing react?

like image 634
moesh Avatar asked Jul 28 '17 00:07

moesh


People also ask

How do you know if a video is finished in video React?

Video React package provide Player component to show video player and also provide onEnded on player to detect video end or not. import { Player } from "video-react"; import "video-react/dist/video-react.

How do you show real time in React?

To show current time and update the seconds in real time with React Native, we can use the setInterval function to update the time every second. to call setInterval with a callback that calls setTime with new Date(). toLocaleString() to set time to the current date time string.

Is React still maintained by Facebook?

React (also known as React. js or ReactJS) is a free and open-source front-end JavaScript library for building user interfaces based on UI components. It is maintained by Meta (formerly Facebook) and a community of individual developers and companies.


1 Answers

You could also use the onEnded media event on the video element. This feature has been supported since 2015; it's much easier to work with and can be used in functional components.

const myCallback = () => (console.log('Video has ended'));

<video autoplay onEnded={() => myCallback()}>
    <source src={[yourVideoURL]} type="video/mp4" />
</video>

Also make sure loop is not true on the video element, because then, the onEnded event will never be fired.

More information here: https://reactjs.org/blog/2015/09/10/react-v0.14-rc1.html

like image 120
Asamoah Avatar answered Sep 22 '22 08:09

Asamoah