[英]How can I get current time of autoplayed html5 mp4 video in javascript and then stop it when time reaches for lets say 5 seconds?
[英]How to check if current time is equal to a particular number before I stop the video in html5 video
我已經在HTML5視頻中編寫了此代碼,以使用時間功能。 如果我將此代碼行設置為
if(vid.currentTime > 5){
vid.pause();
}
視頻會自動暫停,這就是我想要的。 但是如果我將其設置為
if(vid.currentTime == 5){
vid.pause();
}
它拒絕暫停,我希望它在不同時間暫停,以執行不同的操作。 我不知道為什么 我已經研究並嘗試使用谷歌搜索類似情況,但是沒有運氣。 任何幫助將不勝感激。 這是完整的代碼:
window.onload = init;
let vid;
function init() {
vid = document.querySelector("#myPlayer");
vid.ontimeupdate = displayTimeWhileVideoIsPlaying;
}
function playVideo() {
vid.play();
}
function pauseVideo() {
vid.pause();
}
function rewindVideo() {
vid.currentTime = 0
}
function displayTimeWhileVideoIsPlaying(evt) {
console.log(vid.currentTime);
ccc = 5;
if(vid.currentTime > 5){
vid.pause();
}
}
這是我的代碼筆的工作演示鏈接: https ://codepen.io/Okezie/pen/xWOLQd?editors=1011
問題大概在我的代碼筆的第28行上。
您可以跟蹤遇到了哪些暫停點,而不會再次暫停。
var points = [
{ time: 5 , done: false},
{ time: 6 , done: false},
{ time: 8 , done: false},
{ time: 9 , done: false},
];
function displayTimeWhileVideoIsPlaying(evt) {
for(var point of points){
console.log(vid.currentTime);
if(vid.currentTime > point.time){
if(!point.done){
vid.pause();
//vid.currentTime = point.time; //Optional, if you want to pause exact
point.done = true;
}
}
}
!point.done
檢查可防止大於檢查的“不精確性”。 如果代碼先前在5處暫停,則可防止該暫停在5.3、5.4、5.8等處生效。
檢出更新的筆: https : //codepen.io/anon/pen/PRGOxW
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.