Eri*_*yen 3 html javascript video
我正在尝试创建在特定时间点启动本地视频并播放特定持续时间的按钮。我已经让它在某个时间播放,但不确定如何让它播放一段时间。
这是代码。
HTML:
<video id="myvideo" width="1000">
<source src="video.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<div>
<p>
<button id="playme">Play/Pause Video</button>
</p>
<p>
<button id="jump">Jump to 4 seconds</button>
</p>
<p>
<button id="jump2">Jump to 11 seconds</button>
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
Javascript:
var myvideo = document.getElementById('myvideo'),
playbutton = document.getElementById('playme'),
jumplink = document.getElementById('jump'),
jumplink2 = document.getElementById('jump2');
jumplink.addEventListener("click", function (event) {
event.preventDefault();
myvideo.play();
myvideo.pause();
myvideo.currentTime = 4;
myvideo.play();
}, false);
jumplink2.addEventListener("click", function (event) {
event.preventDefault();
myvideo.play();
myvideo.pause();
myvideo.currentTime = 11;
myvideo.play();
}, false);
playbutton.addEventListener("click", function () {
if (myvideo.paused) {
myvideo.play();
} else {
myvideo.pause();
}
}, false);
Run Code Online (Sandbox Code Playgroud)
为了实现这一点,您需要在播放视频时轮询视频的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();
}
Run Code Online (Sandbox Code Playgroud)
更新
让我们看看如何为您的应用实现这一点。
您有两个“跳转”按钮和一个播放按钮。当这些按钮中的任何一个被激活时,您可以调用一个函数,该函数根据id单击按钮的 HTML 设置开始时间和结束时间。这些值可以传递到我上面已经概述的函数中。
首先,为每个按钮分配一个事件侦听器...
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
);
});
Run Code Online (Sandbox Code Playgroud)
在这里,buttonEvents()当您点击三个 HTML 按钮时,每个按钮都会调用一个函数。该功能可能看起来像这样......
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);
}
Run Code Online (Sandbox Code Playgroud)
将您的“播放按钮”函数与上面概述的通用函数相结合,我们将得到如下结果:一个接收开始和结束时间作为参数的函数,并在视频暂停时播放视频,如果正在播放则跳转到新的开始时间。 .
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();
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助。