如何为HTML视频添加缓冲

Ali*_*Zia 5 html javascript jquery html5 html5-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>
Run Code Online (Sandbox Code Playgroud)

我想为他们添加缓冲事件.此外,当我点击视频1时,视频2(如果正在播放)应该自动停止.我怎样才能做到这一点?他们有一个共同的阶级.我要用它们实现id吗?

Sim*_*imo 1

为您的视频提供 ID:

<video width="320" height="240" controls class="myvideo" id="myVideoOne">
<video width="320" height="240" controls class="myvideo" id="myVideoTwo">
Run Code Online (Sandbox Code Playgroud)

用于缓冲:

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));
Run Code Online (Sandbox Code Playgroud)

要自动停止,您可以在 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

video.loading {
    background: black url(/yourGifImg.gif) center center no-repeat;
}
Run Code Online (Sandbox Code Playgroud)

jQuery:

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