126

I'm looking to place a video in an HTML5 page that will begin playing on page-load, and once completed, loop back to the beginning without a break. The video should also NOT have any controls associated with it, and either be compatible with all 'modern' browsers, or have the option of a polyfill.

Previously I would have done this via Flash and FLVPlayback, but I would prefer to steer clear of Flash in the HTML5 sphere. I'm thinking I could use javascript's setTimeout to create a smooth loop, but what should I use to embed the video itself? Is there something out there that will stream the video the way FLVPlayback would?

4 Answers 4

215

The loop attribute should do it:

<video width="320" height="240" autoplay loop muted>
  <source src="movie.mp4" type="video/mp4" />
  <source src="movie.ogg" type="video/ogg" />
  Your browser does not support the video tag.
</video>

The addition of the unintuitive muted attribute is required by Chrome as documented on their dev site.

Should you have a problem with the loop attribute (as we had in the past), listen to the videoEnd event and call the play() method when it fires.

Note1: I'm not sure about the behavior on Apple's iPad/iPhone, because they have some restrictions against autoplay.

Note2: loop="true" and autoplay="autoplay" are deprecated

8
  • 2
    Indeed good advice sir. I ended up using javascript to listen for the videoEnd event, and looping back to the beginning as from what I can tell, the loop attribute is not supported by all browsers. For iOS devices, they don't support autoplay in any way shape or form by iOS 5, so I just used a fallback image for mobile. Thanks again. May 14, 2012 at 23:25
  • 12
    using <video loop="true"> is invalid w3.org/TR/html-markup/video.html - either use <video loop="loop"> or just <video loop> - this rule hold for almost ALL HTML5 tags (either just tagname, or tagname="tagname" for XHTML5)
    – user719662
    Nov 4, 2013 at 18:13
  • @vaxquis thanks for update, you are welcome to edit api changes next time!
    – longi
    Nov 7, 2013 at 13:42
  • 5
    No problem, mate, always happy to help. Still, I wouldn't mix XHTML-type attributes 'autoplay="autoplay"' with HTML-type 'loop' - I'd go with with either <video ... autoplay loop>, parsable as HTML5 or <video ... autoplay="autoplay" loop="loop">, parsable as either HTML5 or XHTML5. mixing styles makes a markup that's still invalid as XHTML but unnecessarily verbose for HTML.
    – user719662
    Nov 8, 2013 at 5:44
  • 3
    Generally, autoplay only works in Safari if you also use muted: webkit.org/blog/7734/auto-play-policy-changes-for-macos
    – andreyrd
    Apr 18, 2018 at 17:57
62

As of April 2018, Chrome (along with several other major browsers) now require the muted attribute too.

Therefore, you should use

<video width="320" height="240" autoplay loop muted>
  <source src="movie.mp4" type="video/mp4" />
</video>
23

For iPhone it works if you add also playsinline so:

<video width="320" height="240" autoplay loop muted playsinline>
  <source src="movie.mp4" type="video/mp4" />
</video>
1
  • 1
    Props for JSX: autoPlay loop muted playsInline
    – pomber
    Apr 8, 2020 at 21:51
5

You can do this the following two ways:

1) Using loop attribute in video element (mentioned in the first answer):

2) and you can use the ended media event:

window.addEventListener('load', function(){
    var newVideo = document.getElementById('videoElementId');
    newVideo.addEventListener('ended', function() {
        this.currentTime = 0;
        this.play();
    }, false);

    newVideo.play();

});

Not the answer you're looking for? Browse other questions tagged or ask your own question.