如果页面在20秒后空闲,则在播放列表中添加新的视频源
if page idle after 20 second new video source append in playlist
我使用jquery和html5显示视频播放列表。我想添加,如果我的页面是空闲的20秒然后下一个视频源添加这是我的Html代码:-
<html>
<body>
<figure id="video_player">
<div id="video_container">
<video controls poster="http://demosthenes.info/assets/images/vid-glacier.jpg">
<source src="http://demosthenes.info/assets/videos/glacier.mp4" type="video/mp4">
<source src="http://demosthenes.info/assets/videos/glacier.webm" type="video/webm">
</video>
</div>
<figcaption>
<a href="http://demosthenes.info/assets/videos/glacier.mp4" class="currentvid">
<img src="http://demosthenes.info/assets/images/vid-glacier.jpg" alt="Athabasca Glacier">
</a>
<a href="http://demosthenes.info/assets/videos/lake.mp4">
<img src="http://demosthenes.info/assets/images/vid-lake.jpg" alt="Athabasca Lake">
</a>
<a href="http://demosthenes.info/assets/videos/mountain.mp4">
<img src="http://demosthenes.info/assets/images/vid-mountain.jpg" alt="Mountain">
</a>
</figcaption>
</figure>
</body>
和我的javascript代码是:-
var video_player = document.getElementById("video_player");
video = video_player.getElementsByTagName("video")[0],
video_links = video_player.getElementsByTagName("figcaption")[0],
source = video.getElementsByTagName("source"),
link_list = [],
vidDir = "http://demosthenes.info/assets/videos/",
currentVid = 0,
allLnks = video_links.children,
lnkNum = allLnks.length;
video.removeAttribute("controls");
video.removeAttribute("poster");
(function() {
function playVid(index) {
video_links.children[index].classList.add("currentvid");
source[1].src = vidDir + link_list[index] + ".webm";
source[0].src = vidDir + link_list[index] + ".mp4";
currentVid = index;
video.load();
video.play();
}
for (var i=0; i<lnkNum; i++) {
var filename = allLnks[i].href;
link_list[i] = filename.match(/([^'/]+)(?='.'w+$)/)[0];
(function(index){
allLnks[i].onclick = function(i){
i.preventDefault();
for (var i=0; i<lnkNum; i++) {
allLnks[i].classList.remove("currentvid");
}
playVid(index);
}
})(i);
}
video.addEventListener('ended', function () {
allLnks[currentVid].classList.remove("currentvid");
if ((currentVid + 1) >= lnkNum) { nextVid = 0 } else { nextVid = currentVid+1 }
playVid(nextVid);
})
video.addEventListener('mouseenter', function() {
video.setAttribute("controls","true");
})
video.addEventListener('mouseleave', function() {
video.removeAttribute("controls");
})
var indexOf = function(needle) {
if(typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function(needle) {
var i = -1, index = -1;
for(i = 0; i < this.length; i++) {
if(this[i] === needle) {
index = i;
break;
}}
return index;
};}
return indexOf.call(this, needle);
};
var focusedLink = document.activeElement;
index = indexOf.call(allLnks, focusedLink);
document.addEventListener('keydown', function(e) {
if (index) {
var focusedElement = document.activeElement;
if (e.keyCode == 40 || e.keyCode == 39) { // down or right cursor
var nextNode = focusedElement.nextElementSibling;
if (nextNode) { nextNode.focus(); } else { video_links.firstElementChild.focus(); }
}
if (e.keyCode == 38 || e.keyCode == 37) { // up or left cursor
var previousNode = focusedElement.previousElementSibling;
if (previousNode) { previousNode.focus(); } else { video_links.lastElementChild.focus(); }
}
}
});
var idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 10000); // 1 minute
console.log("ide..."+idleInterval);
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
console.log("idleTime..."+idleTime);
if(idleTime>=5){
allLnks[currentVid].classList.remove("currentvid");
if ((currentVid + 1) >= lnkNum) { nextVid = 0 } else { nextVid = currentVid+1 }
// playVid(nextVid);
}
}
})();
你可以试试
var idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every sec.
var idleInterval = setInterval(timerIncrement, 1000); // 1 sec
//Zero the idle timer on mouse movement or key press.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1; //add 1 sec idleTime
if (idleTime > 19 ) { // if greater than 20 sec
//add you code where
idleTime = 0; // reset idleTime to 0
}
}
页面加载时,每秒钟调用一次timeIncrement()
函数,每次调用timeIncrement()
函数,增加idleTime
全局变量。如果idleTime
大于19,则加载另一个视频。
您可以根据需要每秒钟或每5秒或每10秒调用一次timeIncrement()
。但是你也需要在函数中分别增加idleTime
。
相关文章:
- Javascript::通过HTML5音频播放器播放列表播放多个音频文件
- Sequelize:属性之间的命名冲突'播放列表'以及关联'播放列表'
- 如何通过点击带有源的按钮将视频添加到播放列表中
- 我可以使用网络爬虫将新的 youtube 视频添加到播放列表吗?
- 集成指南中的“添加播放列表”按钮不起作用
- 如果添加了播放列表,JWPLAYER 播放器不起作用
- 如何为我的嵌入YouTube播放列表添加Facebook共享按钮
- 如何允许用户将播放列表添加到单个 YT 帐户
- SPOTIFY APP API:向播放列表项.sp-item/更改views.js中的item_HEIGHT属性添加填充
- 将本地音乐添加到spotify播放列表中
- 添加轨道播放列表Deezer API
- 选择Jplayer播放列表添加复选框
- 使用Youtube播放器IFrame API添加视频到播放列表
- 在MediaElement Player中动态添加播放列表项
- 是否有一种方法来添加轨道到现有的播放列表在新的spotify API自1.0
- jQuery和Audio.js:动态添加到播放列表中的歌曲,无法播放或识别
- 添加播放列表到iframe标签
- 如何将歌曲添加到jPlayer播放列表的开头
- 如果页面在20秒后空闲,则在播放列表中添加新的视频源
- JW Player 6:在播放列表中添加多个源