HTML5 Video not looping

后端 未结 5 1186
粉色の甜心
粉色の甜心 2021-02-04 19:09

I\'m having a weird problem. My two versions of chrome(regular & canary) refuse to loop the video i\'m showing. Or well, sometimes they loop it twice and stops after that. W

相关标签:
5条回答
  • 2021-02-04 19:12

    This happens only if you run your site locally...
    And i had same problem with Chrome but i found solution in XAMP local server...

    you can use any local server that you want(like wamp and etc)... but the site must be in server root directory... this way chrome understands that the video comes from server and not from local mashing

    0 讨论(0)
  • 2021-02-04 19:17

    I have recently had some issue with this myself. It turned out to be something to do with my site being on localhost. When I move the site to my production server and tested remotely it all worked as expected.

    To force it to work on localhost, I used the solution from Joakim Bananskal, but playing the video cause an error because it was already trying to play it, so I just had to reset the video first using load().

    Having it set to loop also seemed to cause an issue, because the video never fired the ended event.

    My final solution for localhost is below:

    $("video").each(function () {
        this.loop = false;
        this.onended = function () {
            this.load();
        };
        this.play();
    });
    

    with this HTML:

    <video preload="auto" autoplay>
        <source src="/video.mp4" type="video/mp4">
        <source src="video.webm" type="video/webm">
        <source src="/video.ogg" type="video/ogg">
        <img src="/backupImage.png" />
    </video>
    
    0 讨论(0)
  • 2021-02-04 19:20

    I've also found that Chrome chokes if the MP4 keyframes aren't set properly. For example, in After Effects, in Output, if the key frame distance is left as "auto" the video doesn't loop properly.

    My suspicion is that Chrome needs evenly divisible key frames within the length of the video, as in not ending between keyframes. i.e. if your video is 24 frames, make your key frame distance evenly divisible, 4 for example.

    After Effects Render Output Settings

    0 讨论(0)
  • 2021-02-04 19:23

    This one is solved, and here's the solution in my case:

    The video had a resolution that was too big. Even if the bitrate was low, chrome didn't want to do it. Resized it to be 720p and it worked perfectly.

    Other suggested solutions if you're having problems:

    • Set the correct content-type, including codec.
    • Make sure you're in a browser that supports your file-type. For live things, always use atleast both .mp4 and .ogg, and include .webm for security.
    • Set it to loop via javascript. This is also a good fallback for browsers not being okay with the loop attribute on the video tag (main example being ipad's). Below is some example code that I copied of a site yesterday (sorry, can't remember the source)

      var myVideo = document.getElementById('video');
      if (typeof myVideo.loop == 'boolean') { // loop supported
          myVideo.loop = true;
      } else { // loop property not supported
          myVideo.on('ended', function () {
          this.currentTime = 0;
          this.play();
          }, false);
      }
      myVideo.play();
      
    0 讨论(0)
  • 2021-02-04 19:29

    For some reason I had issues with 'ended' event binding.

    Here is how I fixed it:

    1. removed 'loop' attr from video.
    2. added onended to invoke replay()

       <video autoplay='true' onended="replay()"></video> 
    3. defined replay() as below:

      function replay() {
          console.log('video ended');
          document.getElementsByTagName('video').currentTime = 0;
          document.getElementsByTagName('video')[0].play();
      }
    0 讨论(0)
提交回复
热议问题