11

I have created a react component to play videos. The component is playing video just fine but the callback is not being fired.

class VideoPlayer extends React.Component {
  componentWillMount () {
      console.log('Mounting here');
  }

  changeThis () {
    console.log("Ended");
  }

  render () {
    var notice = this.props.notice;
    var p = "./data/video/" + notice.path + '?' + Math.random();
    return (
      <div key={notice.id}>
      <video className="image" alt={notice.description} onEnded={this.changeThis.bind(this)} controls autoPlay>
        <source src={p} type="video/mp4" />
        No suppoty
      </video>
      </div>
    )
  }
}

The onEnded function is not being called. What am I doing wrong.

2 Answers 2

25

ReactJS (0.14.0-rc1) now supports media events (video and audio): https://facebook.github.io/react/blog/2015/09/10/react-v0.14-rc1.html

I've tested onEnded and onTimeUpdate - works great!

Sign up to request clarification or add additional context in comments.

2 Comments

Do you have an example of this? Possibly using hooks?
Hello ; +1, do you have example ? Thanks
6

Small tip for the next person that has the same problem as I did onEnded was not triggering because I had the loop attribute as well.

In my case I already had it on loop and then there is a play button. So I ended up with this:

const handlePlayPress = () => {
  vidRef.current.pause();
  vidRef.current.currentTime = 0;
  vidRef.current.muted = false;
  vidRef.current.loop = false;
  vidRef.current.play();
};

const handleVideoEnded = () => {
  console.log('Video ended!');
};

<video
  ref={vidRef}
  muted
  autoPlay
  loop
  onEnded={handleVideoEnded}>
    <source src="/videos/video.webm" type="video/webm" />
    <source src="/videos/video.mp4" type="video/mp4" />
    Your browser does not support the video tag.
</video>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.