如何为HTML视频添加缓冲

时间:2016-09-01 09:05:12

标签: javascript jquery html 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>

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

1 个答案:

答案 0 :(得分:1)

为您的视频提供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')
});
相关问题