Play infinitely looping video on-load in HTML5

前端 未结 4 1725
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 22:42

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

相关标签:
4条回答
  • 2020-11-29 23:09

    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>
    
    0 讨论(0)
  • 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();
    
    });
    
    0 讨论(0)
  • 2020-11-29 23:29

    The loop attribute should do it:

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

    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

    0 讨论(0)
  • 2020-11-29 23:32

    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>
    
    0 讨论(0)
提交回复
热议问题