为了实现这一点,您需要轮询视频的currentTime
属性,因为它正在播放,也许像这样的通用代码......
var button = document.getElementById('play')
var video = document.getElementById('video');
var startTime = 10;
var endTime = 20;
button.addEventListener('click', playVideo, !1);
function playVideo(e) {
function checkTime() {
if (video.currentTime >= endTime) {
video.pause();
} else {
/* call checkTime every 1/10th
second until endTime */
setTimeout(checkTime, 100);
}
}
video.currentTime = startTime;
video.play();
checkTime();
}
UPDATE
让我们看看如何为您的应用程序实现此功能。
您有两个“跳跃”按钮和一个播放按钮。当激活这些按钮中的任何一个时,您可以调用一个函数,根据 HTML 设置开始时间和结束时间id
单击的按钮的。这些值可以传递到类似我上面已经概述的函数中。
首先,为每个按钮分配一个事件监听器......
var myvideo = document.getElementById('myvideo');
/* add the same event and
handler function to each
of the three buttons */
var buttons = ['playme','jump','jump2'];
buttons.forEach(function(bn) {
document.getElementById(bn).addEventListener(
'click', buttonEvents, !1
);
});
这里三个 HTML 按钮中的每一个都会调用一个buttonEvents()
单击时的功能。该函数可能看起来像这样......
function buttonEvents(e) {
/* get the id of the clicked button */
var element_id = e.target.id;
/* E.G. element_id = 'playme', 'jump', or 'jump2' */
/* declare variables before setting them */
var timeStart = 0;
var timeEnd = 0;
/* set start and end values depending
on which button was clicked */
switch(element_id) {
case 'playme':
/* example values... */
timeStart = 0;
timeEnd = 100;
break;
case 'jump':
timeStart = 4;
timeEnd = 12;
break;
case 'jump2':
timeStart = 12;
timeEnd = 24;
}
/* call 'playVideo()' */
playVideo(timeStart, timeEnd);
}
将您的“播放按钮”功能与上面概述的一般功能相结合将为我们提供这样的功能:一个函数接收开始和结束时间作为参数,并在视频暂停时播放视频,或者在播放时跳转到新的开始时间。 。
function playVideo(startTime, endTime) {
function checkTime() {
if (myvideo.currentTime >= endTime) {
myvideo.pause();
} else {
/* call checkTime every 1/10th
second until endTime */
setTimeout(checkTime, 100);
}
}
/* stop if playing (otherwise ignored) */
myvideo.pause();
/* set video start time */
myvideo.currentTime = startTime;
/* play video */
myvideo.play();
/* check the current time and
pause IF/WHEN endTime is reached */
checkTime();
}
希望有帮助。