有关HTML5 Video对象的ontimeupdate事件的问题

来源:互联网 发布:电脑转盘抽奖软件 编辑:程序博客网 时间:2024/04/29 17:01

转载地址:http://www.cnblogs.com/jaxu/p/3155915.html


日前在做一个视频播放的页面,其中用到了HTML5的Video对象,这个是HTML5中新增的一个对象,支持多种不同格式的视频在线播放,功能比较强大,而且还扩展了许多事件,可以通过JavaScript脚本来对视频播放进行控制。参考下面两个链接:

http://msdn.microsoft.com/en-us/library/windows/apps/hh465962.aspx

http://www.w3school.com.cn/html5/tag_video.asp

  Video对象可以通过ontimeupdate事件来报告当前的播放进度,同时通过该事件还可以根据视频播放的情况来控制页面上的其它元素,例如随着视频播放的进度来切换章节、显示额外信息等。下面是一个例子:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />    <title></title></head><body>    <script type="text/javascript">        function timeUpdate() {            document.getElementById('time').innerHTML = video.currentTime;        }        function durationChange() {            document.getElementById('duration').innerHTML = video.duration;        }        function seekVideo() {            document.getElementById('video').currentTime = document.getElementById('seekText').value;        }        window.onload = function () {            var videoPlayer = document.getElementById("video");            videoPlayer.ontimeupdate = function () { timeUpdate(); };        };    </script>    <div>        <video id="video" controls="controls"            poster="./images/videoground1.png"            src="./videoSource/video1.mp4" width="450px" height="320px"            ondurationchange="durationChange()" />    </div>    <div>Time: <span id="time">0</span> of <span id="duration">0</span> seconds.</div>    <div>        <input type="text" id="seekText" />        <input type="button" id="seekBtn" value="Seek Video" onclick="seekVideo();" />    </div></body></html>

当然你也可以像在页面上使用其它元素一样,给Video对象动态添加属性或者挂事件,如

video.ontimeupdate = function () { getCurrentVideoPosition(); };

不过上面这行代码貌似在Chrome上无效,可以使用addEventListener来代替它:

videoPlayer.addEventListener("timeupdate", function () { getCurrentVideoPosition(); }, false);


不知道是什么原因在Chrome上不能直接将ontimeupdate事件挂在Video元素上,而必须通过addEventListener方法来添加事件。不过addEventListener也兼容IE和Firefox浏览器,所以应该是通过的。


0 0
原创粉丝点击