开满天机
<style>#v{width:400px;height:300px}</style>
<video id="v" controls></video><br />
<input type="file" onchange="setVideoSrc(this)" />
<script>
function getDuration() {
if (!isNaN(v.duration)) {
clearInterval(timer);
alert(v.duration)
}
else {
tryTime++;
//超过获取视频长度最大重试次数
if (tryTime >= maxTry) { clearInterval(timer); alert('无法获取视频长度!');}
}
}
var timer,tryTime,maxTry=5,delay=500;
function setVideoSrc(f) {
clearInterval(timer);
var url = URL.createObjectURL(f.files[0]);
v.src = url;
tryTime = 0;
timer = setInterval(getDuration, delay);//直接获取不到duration,会NaN,计时器来获取
}
</script>