Updating source URL on HTML5 video with React

I want to update the source tag in a HTML5 video element so that when I click a button, whatever’s playing switches to a new video.

I have a Clip component that returns an HTML5 video element, with the source URL provided via props.

function Clip(props) {
  return (
    <video width="320" height="240" controls autoPlay>
      <source src={props.url} />
    </video>
  )
}

I also have a Movie component, which contains multiple URLs, each corresponding to a section of the movie. It also contains a position attribute, which acts like an iterator by remembering which section is currently playing.

class Movie extends Component {
  constructor() {
    super();
    this.state = {
      sections: [
        'https://my-bucket.s3.amazonaws.com/vid1.mp4',
        'https://my-bucket.s3.amazonaws.com/vid2.mp4'
      ],
      position: 0
    };
  }
  render() {
    const sections = this.state.sections
    const position = this.state.position

    return (
      <div>
        {position}
        <Clip url={sections[position]} />
        <button onClick={() => this.updatePosition()}>Next</button>
      </div>
    )
  }
  updatePosition() {
    const position = this.state.position + 1;
    this.setState({ position: position });
  }
}

The Movie component renders a Clip component and a “Next” button. When the Next button gets clicked, I want to update the position attribute and re-render the HTML5 video element using the next URL from sections.

As of now, when I hit Next the HTML5 video source tag updates, but the element continues playing the video from the previous URL. Can anyone help me figure out how to reset the video element?

Update: I created a JSFiddle, which you can see here.

Update 2: Updating the src attribute on the video element works!

161 thoughts on “Updating source URL on HTML5 video with React”

  1. Pingback: start keto diet
  2. Pingback: bimatoprost 0.01
  3. Pingback: cost of olumiant
  4. Pingback: 4mg zanaflex
  5. Pingback: nolvadex coupon
  6. Thanks for your personal marvelous posting!
    I seriously enjoyed reading it, you’re a great author.
    I will remember to bookmark your blog and definitely will come back in the foreseeable future.
    I want to encourage you to ultimately continue your great job, have a nice evening!

    Reply
  7. Howdy! This is my 1st comment here so I just wanted to give a quick shout out
    and tell you I genuinely enjoy reading your posts. Can you recommend any other blogs/websites/forums that cover the
    same subjects? Appreciate it!

    Reply
  8. Hey! This is my first visit to your blog! We are a team of
    volunteers and starting a new project in a community in the same niche.

    Your blog provided us valuable information to work
    on. You have done a extraordinary job!

    Reply
  9. I would like to thank you for the efforts you have put in writing
    this blog. I am hoping to see the same high-grade content from you in the future as well.

    In truth, your creative writing abilities has inspired me to get my very own website now 😉

    Reply
  10. What i do not understood is if truth be told how you’re
    no longer actually much more neatly-favored than you might be
    now. You are very intelligent. You know therefore significantly relating to this matter,
    produced me for my part consider it from numerous various
    angles. Its like women and men are not involved unless it’s something to do
    with Girl gaga! Your own stuffs great. Always deal with it up!

    Reply
  11. Hi! I’m at work surfing around your blog from my new iphone 3gs!
    Just wanted to say I love reading your blog and look forward to all your posts!
    Carry on the superb work!

    Reply
  12. You really make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand.
    It seems too complicated and extremely broad for me. I am looking forward for your next post, I’ll try to get
    the hang of it!

    Reply
  13. obviously like your web-site however you have to test the spelling on several of your posts.
    Several of them are rife with spelling issues and I to find
    it very bothersome to tell the truth on the other
    hand I’ll certainly come back again.

    Reply
  14. Hello very nice blog!! Man .. Excellent .. Superb .. I’ll bookmark your blog and take the feeds additionally?
    I’m happy to search out numerous helpful info right here
    in the submit, we’d like work out more strategies
    in this regard, thank you for sharing. . . . .
    .

    Look into my site: special

    Reply
  15. I do not know if it’s just me or if everybody else experiencing issues with your blog.
    It looks like some of the written text on your content are running off the screen. Can someone else please provide feedback and let me know if this is happening to them too?
    This may be a problem with my internet browser because I’ve
    had this happen before. Appreciate it

    Reply

Leave a Comment