Home > Web Front-end > H5 Tutorial > html5 returns the duration attribute of the current audio/video in seconds

html5 returns the duration attribute of the current audio/video in seconds

黄舟
Release: 2017-11-08 13:57:21
Original
4167 people have browsed it

实例

获得当前视频的长度:

myVid=document.getElementById("video1");
alert(myVid.duration);
Copy after login

定义和用法

duration 属性返回当前音频/视频的长度,以秒计。

如果未设置音频/视频,则返回 NaN (Not-a-Number)。

浏览器支持

所有主流浏览器都支持 duration 属性。

注释:Internet Explorer 8 或更早的浏览器不支持该属性。

语法

audio|video.duration
Copy after login

技术细节

返回值数字值,表示音频/视频的长度,以秒计。

根据JavaScript高级程序设计中P489页的程序,我写了以下程序

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<p class="mediaplayer">
<p class="video">
<video  id="player" poster="E:\1.jpg" width="300" height="200"  src="太阳E15.mp4"> 
Video player is not available.
</video>
</p>
<p class="controls">
<input type="button" value="Play" id="video-btn">
<span id="curtime">0</span>/<span id="duration">0</span>
</p>
</p>
<script type="text/javascript">
window.onload=function(){
var oPlayer=document.getElementById(&#39;player&#39;);
var oBtn=document.getElementById("video-btn");
var oCurtime=document.getElementById(&#39;curtime&#39;);
var oDuration=document.getElementById(&#39;duration&#39;);
oDuration.innerHTML=oPlayer.duration;
oBtn.onclick=function(){
if(oPlayer.paused){
oPlayer.play();
oBtn.value="Pause";
}else{
oPlayer.pause();
oBtn.value="Play";
}
};
setInterval(function(){
oCurtime.innerHTML=oPlayer.currentTime;
},250);
};
</script>
</body>
</html>
Copy after login

由于IE8及其之前的版本不支持video标签,所以不显示。

但是在IE9,10,11支持的情况下,为什么会显示

并且,使用alert(oPlayer.duration);页面是能够弹出该视频的时间的。

但是为什么oDuration.innerHTML=oPlayer.duration;这句话会显示NaN?

对此,我又尝试了一下代码

var duration1=oPlayer.duration;
alert(duration1);//NaN
alert(oPlayer.duration);//3625.982
Copy after login

由上面的结果,可以看出将oPlayer.duration的值赋给一个变量后这个变量的值是NaN。但是为什么?根据定义oPlayer.duration属性返回的是一个数值,为什么数值赋给一个变量会变成NaN?现在还不懂。


The above is the detailed content of html5 returns the duration attribute of the current audio/video in seconds. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template