简体   繁体   English

页面上的所有视频均已完全加载时执行javascript

[英]Execute javascript when all videos on page have been fully loaded

I have modified this script to accept videos instead of images: 我已修改此脚本以接受视频而不是图像:

http://www.catchmyfame.com/catchmyfame-jquery-plugins/jquery-beforeafter-plugin/ http://www.catchmyfame.com/catchmyfame-jquery-plugins/jquery-beforeafter-plugin/

Locally, it works fine, but when I upload to a server, the videos are not synced. 在本地,它工作正常,但是当我上传到服务器时,视频不同步。 The first video starts earlier than the other one. 第一个视频比另一个视频更早开始。 Check it out: 看看这个:

http://amarsyla.com/sandbox/beforeafter/ http://amarsyla.com/sandbox/beforeafter/

HTML: HTML:

<div id="container">
    <div>
        <video alt="before" autoplay="true" loop="true" width="600" height="366">
            <source src="before.mp4" type="video/mp4;">
        </video>
    </div>
    <div>
        <video alt="after" autoplay="true" loop="true" width="600" height="366">
            <source src="after.mp4" type="video/mp4;">
        </video>
    </div>
</div>

I initialize the plugin using this code: 我使用以下代码初始化插件:

$(window).load(function() {
    $('#container').beforeAfter();
});

Obviously, the window.load doesn't do the job. 显然,window.load不能完成这项工作。 I need a JavaScript event or something similar which will be triggered that both of the videos have been loaded and they can start playing simultaneously with each other. 我需要一个JavaScript事件或类似的事件,这将触发两个视频都已加载并且它们可以同时开始同时播放。 I want the videos to be in perfect sync with each other, so each of them starts at the same time, and I thought this would be possible by initializing the plugin after both videos have been fully loaded. 我希望视频能够彼此完美同步,因此每个视频都在同一时间开始,并且我认为这可以通过在两个视频完全加载后初始化插件来实现。 I've tried this: 我已经试过了:

var vid = document.getElementById("myVideo");
vid.oncanplaythrough = function() {
    alert("Can play through video without stopping");
};

That doesn't work as I expected. 这不符合我的预期。 It doesn't always fire. 它并不总是会触发。 Any help would be appreciated. 任何帮助,将不胜感激。

The code in your question is different from the code at the link, so I'll go by the latter since that's the one actually running. 您问题中的代码与链接中的代码不同,因此我将使用后者,因为那是实际运行的代码。

It looks like what's happening is that even once the videos are playing, they're losing synchronization a few seconds later when they have to wait for more data from the network. 看起来正在发生的事情是,即使视频播放后,它们又不得不在几秒钟后失去同步,这时他们不得不等待来自网络的更多数据。 One might think that "oncanplaythrough" would be enough to assume that the videos are sufficiently buffered to play all the way through without pausing, it's not always the case. 有人可能会认为“ oncanplaythrough”足以假设视频已被充分缓冲,可以一直播放而不暂停,情况并非总是如此。

In theory, "canplaythrough" fires when the browser guesses that data is coming in faster than you're playing it, as opposed to "canplay" which fires when there is just enough data to show one or two frames from the current time. 从理论上讲,当浏览器猜测输入数据的速度比您播放数据快时,就会触发“ canplaythrough”,而当仅有足够的数据可以显示当前时间的一两个帧时,就会触发“ canplaythrough”。 But Chrome fires "canplaythrough" immediately after "canplay" so you can't count on it. 但是Chrome会在“可播放”之后立即触发“可播放”,因此您不能指望它。 Even on other better-behaved browsers, it's still possible that the data transfer starts fast and then slows down after the event fires. 即使在其他性能较好的浏览器上,事件触发后,数据传输仍可能开始快速启动,然后变慢。

So that means that you have to continuously watch for any "waiting" events on either video and pause them both until they catch up again. 因此,这意味着您必须连续观看任一视频上的任何“等待”事件,并将它们暂停,直到它们再次出现。

Here's an example you can use as a reference to get you started: http://code.chirls.com/whiteknuckles/ 这是一个示例,您可以用作入门参考: http : //code.chirls.com/whiteknuckles/

It's very old code and not my best work. 这是很旧的代码,不是我的最佳工作。 I would do it differently if I wrote it today, but it seems to work reasonably well. 如果我今天写它,我会做不同的事情,但是它似乎工作得很好。

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

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