gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:39:22 25 4
gpt4 key购买 nike

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

<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;
}

j查询:

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

关于javascript - 如何为我的 HTML 视频添加缓冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com