简体   繁体   English

无缝的HTML5视频循环

[英]Seamless HTML5 Video Loop

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. 我创建了一个4秒的视频剪辑,可以在编辑器中无缝循环。 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. 但是,当剪辑通过Safari,Chrome或Firefox在页面中运行时,从结束回到开始播放时会有一个小但明显的暂停。

I have tried using the loop and preload attributes both together and independently. 我尝试过将loop和preload属性放在一起并独立使用。

I have also tried the following javascript: 我也试过以下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? 我对任何想法持开放态度?

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 . 另外,公平警告: 我的回答使用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. 看起来视频循环中的轻微停顿是因为html5视频从一个位置到另一个位置需要时间。 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. 使用javascript克隆标记,并让克隆隐藏,暂停,并在开头。 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() . 是的,我使用了clone.hidden = true而不是$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. 这样,DOM中只显示了哪个视频正在显示,但实际上没有任何搜索必须发生,直到克隆开始播放。 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. 我添加- .5的原因是因为根据我的经验, currentTime实际上从未达到与视频对象的duration相同的值。 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. 在我的情况下,我可以在最后斩断半秒,但在你的情况下,你可能想要在工作时尽可能小地定制.5值。

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 comes up with a better way to detect exactly when the video ends, please comment and I will edit this answer. 缺点是.5 ,所以如果有人想出更好的方法来确切地检测视频何时结束,请发表评论,我将编辑此答案。

I've found that Firefox will stutter while looping if I'm using mp4 or ogv files. 我发现如果我使用mp4或ogv文件,Firefox会在循环时断断续续。 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. 我改变了我的页面中的代码,首先尝试使用webm文件,然后突然口吃不见了,视频无缝循环,或者至少足够接近,我没有看到问题。 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. 如果你没有给出一个镜头,那么尝试将文件转换为webm格式并加载它是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM