最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Seamless HTML5 Video Loop - Stack Overflow

programmeradmin2浏览0评论

I have searched extensively to find a solution to this but have not succeeded. I have created a 4 second video clip that loops seamlessly in an editor. However when the clip runs in a page via Safari, Chrome or Firefox there is a small but noticeable pause in the playback from end back to beginning.

I have tried using the loop and preload attributes both together and independently.

I have also tried the following javascript:

loopVid.play();
loopVid.addEventListener("timeupdate", function() {
    if (loopVid.currentTime >= 4) {
        loopVid.currentTime = 0;
        loopVid.play();
    }
}

But in all cases the momentary pause remains and spoils the effect. I'm open to any ideas?

I have searched extensively to find a solution to this but have not succeeded. I have created a 4 second video clip that loops seamlessly in an editor. However when the clip runs in a page via Safari, Chrome or Firefox there is a small but noticeable pause in the playback from end back to beginning.

I have tried using the loop and preload attributes both together and independently.

I have also tried the following javascript:

loopVid.play();
loopVid.addEventListener("timeupdate", function() {
    if (loopVid.currentTime >= 4) {
        loopVid.currentTime = 0;
        loopVid.play();
    }
}

But in all cases the momentary pause remains and spoils the effect. I'm open to any ideas?

Share Improve this question edited Mar 3, 2015 at 10:53 user1693593 asked Mar 3, 2015 at 10:49 SteveSteve 511 silver badge2 bronze badges 4
  • This answer may be of help: stackoverflow./questions/17930964/… – user1693593 Commented Mar 3, 2015 at 10:53
  • You can also try setting the loop property to true wo/setting time (and setting a new time will also trigger an async event). – user1693593 Commented Mar 3, 2015 at 11:02
  • @KenFyrstenberg: Thanks for that Ken. I did read your previous answer, but as you stated your solution just reduced the glitch I was hoping that a fully seamless solution had since presented itself by now. I appreciate the input though. I had already tried your loop suggestion without success. – Steve Commented Mar 4, 2015 at 20:28
  • 2 It's a limit of the browser(s) unfortunately. Not much we can do about it - the only option left is to create a long running pre-looped video stream - this of course will add a traffic hit on the site. – user1693593 Commented Mar 4, 2015 at 21:12
Add a ment  | 

2 Answers 2

Reset to default 6

Since this question is a top search result in Google, but doesn't "technically" have an answer yet, I'd like to contribute my solution, which works, but has a drawback. Also, fair warning: my answer uses jQuery.

It seems the slight pause in the video loop is because it takes time for html5 video to seek from one position to another. So it won't help anything to try to tell the video to jump to the beginning when it ends, because the seek will still happen. So here's my idea:

Use javascript to clone the tag, and have the clone sit hidden, paused, and at the beginning. Something like this:

var $video = $("video");
var $clone = $video.clone();
$video.after($clone);

var video = $video[0];
var clone = $clone[0];

clone.hidden = true;
clone.pause();
clone.currentTime = 0;

Yes, I used clone.hidden = true instead of $clone.hide(). Sorry.

Anyway, after this the idea is to detect when the original video ends, and then switch to the clone and play it. That way there is only a change in the DOM as to which video is being shown, but there is actually no seeking that has to happen until after the clone starts playing. So after you hide the original video and play the clone, you seek the original back to the beginning and pause it. And then you add the same functionality to the clone so that it switches to the original video when it's done, etc. Just flip flopping back and forth.

Example:

video.ontimeupdate = function() {
    if (video.currentTime >= video.duration - .5) {
        clone.play();
        video.hidden = true;
        clone.hidden = false;
        video.pause();
        video.currentTime = 0;
    }
}

clone.ontimeupdate = function() {
    if (clone.currentTime >= clone.duration - .5) {
        video.play();
        clone.hidden = true;
        video.hidden = false;
        clone.pause();
        clone.currentTime = 0;
    }
}

The reason I add the - .5 is because in my experience, currentTime never actually reaches the same value as duration for a video object. It gets pretty close, but never quite there. In my case I can afford to chop half a second off the end, but in your case you might want to tailor that .5 value to be as small as possible while still working.

So here's my entire code that I have on my page:

!function($){
$(document).ready(function() {

$("video").each(function($index) {
    var $video = $(this);
    var $clone = $video.clone();
    $video.after($clone);

    var video = $video[0];
    var clone = $clone[0];

    clone.hidden = true;
    clone.pause();
    clone.currentTime = 0;

    video.ontimeupdate = function() {
        if (video.currentTime >= video.duration - .5) {
            clone.play();
            video.hidden = true;
            clone.hidden = false;
            video.pause();
            video.currentTime = 0;
        }
    }

    clone.ontimeupdate = function() {
        if (clone.currentTime >= clone.duration - .5) {
            video.play();
            clone.hidden = true;
            video.hidden = false;
            clone.pause();
            clone.currentTime = 0;
        }
    }

});

});
}(jQuery);

I hope this will be useful for somebody. It works really well for my application. The drawback is that .5, so if someone es up with a better way to detect exactly when the video ends, please ment and I will edit this answer.

I've found that Firefox will stutter while looping if I'm using mp4 or ogv files. I changed the code in my page to try using a webm file first instead, and suddenly the stutter was gone and the video looped seamlessly, or at least close enough that I didn't see an issue. If you haven't given that a shot, it could be worth it to try converting the file to a webm format and loading that instead.

发布评论

评论列表(0)

  1. 暂无评论