use*_*420 5 html javascript video jquery
我无法让视频在 2 秒后循环播放。它应该在视频暂停后循环播放。我不确定是否会实现这一点。该功能工作正常,但不循环。
function playVideo() {
var starttime = 0; // start at 0 seconds
var endtime = 2; // stop at 2 seconds
var video = document.getElementById('videoElm');
video.addEventListener("timeupdate", function() {
if (this.currentTime >= endtime) {
this.pause();
// SHOULD LOOP HERE?
}
}, false);
//suppose that video src has been already set properly
video.load();
video.play();
try {
video.currentTime = starttime;
} catch (ex) {
//handle exceptions here
}
}
playVideo();Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<video id="videoElm" autoplay muted controls loop>
<source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>
</div>Run Code Online (Sandbox Code Playgroud)
如果您希望视频在 2 秒后立即返回开头并继续播放,只需设置currentTime回处理程序0内即可timeupdate。您当前的逻辑设置currentTime位置错误,并且try/catch周围的设置是不必要的。尝试这个:
var starttime = 0; // start at 0 seconds
var endtime = 2; // stop at 2 seconds
var video = document.getElementById('videoElm');
function playVideo() {
video.addEventListener("timeupdate", function() {
if (this.currentTime >= endtime) {
this.currentTime = 0; // change time index here
}
}, false);
video.load();
video.play();
}
playVideo();Run Code Online (Sandbox Code Playgroud)
<div>
<video id="videoElm" autoplay muted controls loop>
<source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/webm">
</video>
</div>Run Code Online (Sandbox Code Playgroud)