如何为我的 HTML 视频添加缓冲

How can I add buffering to my HTML video

我在一个页面上有多个视频。

<div class="row text-center">
    <video width="320" height="240" controls class="myvideo">
        <source src="http://www.html5videoplayer.net/videos/toystory.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
</div>

<div class="row text-center">
    <video width="320" height="240" controls class="myvideo">
        <source src="http://www.html5videoplayer.net/videos/fantasy.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
</div>

我想给它们添加缓冲事件。此外,当我单击视频 1 时,视频 2(如果正在播放)应该会自动停止。我怎样才能做到这一点?他们有一个共同点class。我必须用它们来实现 ID 吗?

为您的视频提供 ID:

<video width="320" height="240" controls class="myvideo" id="myVideoOne">
<video width="320" height="240" controls class="myvideo" id="myVideoTwo">

用于缓冲:

var vid = document.getElementById("myVideoOne");
alert("Start: " + vid.buffered.start(0)
    + " End: " + vid.buffered.end(0));

var vid = document.getElementById("myVideoTwo");
alert("Start: " + vid.buffered.start(0)
    + " End: " + vid.buffered.end(0));

要自动停止,您可以在 videoPlay1 和 videoPlay2 函数中使用它:

function videoPlay1() {
    var video1 = document.getElementById("myVideoOne");
    var video2 = document.getElementById("myVideotwo");

    if (video1.paused) {
        video1.play();
        video2.pause();
    } else {
        video1.pause();
        video2.pause();
    }
}

function videoPlay2() {
    var video1 = document.getElementById("myVideoOne");
    var video2 = document.getElementById("myVideotwo");

    if (video2.paused) {
        video2.play();
        video1.pause();
    } else {
        video1.pause();
        video2.pause();
    }
}

示例:要为 myVideoOne 设置加载程序,您可以使用此 jquery: css:

video.loading {
    background: black url(/yourGifImg.gif) center center no-repeat;
}

jquery:

$('#myVideoOne').on('loadstart', function (event) {
    $(this).addClass('loading')
});
$('#myVideoOne').on('canplay', function (event) {
    $(this).removeClass('loading')
});