HTML5:比较video.buffered.end(0)和video.duration
我试图在使用jQuery淡入播放器之前比较视频是否已完全下载。为此,我使用了一个使用setInterval()函数的循环,并比较了视频属性“buffered.end(0)”和“duration”,但两者的长度不同,因为第一个包含的小数位数比第二个多(3):HTML5:比较video.buffered.end(0)和video.duration,html,html5-video,setinterval,Html,Html5 Video,Setinterval,我试图在使用jQuery淡入播放器之前比较视频是否已完全下载。为此,我使用了一个使用setInterval()函数的循环,并比较了视频属性“buffered.end(0)”和“duration”,但两者的长度不同,因为第一个包含的小数位数比第二个多(3): 25.946847656 > 25.946847 在我展示播放器之前,有没有办法检查视频是否已完全下载 以下是我目前掌握的情况: var playerObject = { init : function(obj) {
25.946847656 > 25.946847
在我展示播放器之前,有没有办法检查视频是否已完全下载
以下是我目前掌握的情况:
var playerObject = {
init : function(obj) {
"use strict";
if (obj.length > 0) {
if (obj.length > 1) {
jQuery.each(obj, function() {
playerObject.start($(this));
});
} else {
playerObject.start(obj);
}
}
},
start : function(obj) {
"use strict";
var thisTime = window.setInterval(function() {
if (obj[0].buffered.end(0) === obj[0].duration) {
window.clearInterval(thisTime);
obj.fadeIn(200);
playerObject.create(obj);
}
}, 1000);
},
create : function(obj) {
"use strict";
obj.live('click', function() {
var thisVideo = $(this)[0];
if (thisVideo.paused === false) {
thisVideo.pause();
} else {
thisVideo.play();
}
});
playerObject.endVideo(obj);
},
endVideo : function(obj) {
"use strict";
var thisTime = window.setInterval(function() {
if (obj[0].ended == true) {
window.clearInterval(thisTime);
obj.fadeOut(200);
}
}, 1000);
}
};
$(function() {
"use strict";
playerObject.init($('.player'));
});
我现在修改了start()方法,只需将两者相除,将结果四舍五入并与1进行比较,这将表明它们都是相同的:
start : function(obj) {
"use strict";
var thisTime = window.setInterval(function() {
if (Math.round(obj[0].buffered.end(0) / obj[0].duration) === 1) {
window.clearInterval(thisTime);
obj.fadeIn(200);
playerObject.create(obj);
}
}, 1000);
}
HTML 5规范中似乎没有任何内容会在下载/缓冲完成后触发。但是,一旦浏览器确定下载的媒体数量足够,则会触发该命令,这样就可以播放到最后,而无需停止并缓冲更多的媒体。谢谢西蒙-值得知道。