最近在做一个项目,要求监控视频的播放事件并能够打印LOG日志,经过一番思索,使用javascript实现了此功能,代码如下: HTML: 复制代码 代码如下: Multi Source http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> http://www.w3school.com.cn/i/movie.mp4" controls autoplay> JS: 复制代码 代码如下: <br /> window.addEventListener("load",getVideoEvent);<br /> function getVideoEvent(){<br /> var videoes=document.getElementsByTagName("video");<br /> for (var i = 0; i < videoes.length; i++) {<br /> showEventLog("video"+(i+1),videoes[i]);<br /> }<br /> }<br /> function showEventLog(videoNum,Media){<br /> eventTester = function(e){<br /> Media.addEventListener(e,function(){<br /> console.log(videoNum+":"+e);<br /> });<br /> }<br /> eventTester("loadstart"); //客户端开始请求数据<br /> eventTester("progress"); //客户端正在请求数据<br /> eventTester("suspend"); //延迟下载<br /> eventTester("abort"); //客户端主动终止下载(不是因为错误引起),<br /> eventTester("error"); //请求数据时遇到错误<br /> eventTester("stalled"); //网速失速<br /> eventTester("play"); //play()和autoplay开始播放时触发<br /> eventTester("pause"); //pause()触发<br /> eventTester("loadedmetadata"); //成功获取资源长度<br /> eventTester("loadeddata"); //<br /> eventTester("waiting"); //等待数据,并非错误<br /> eventTester("playing"); //开始回放<br /> eventTester("canplay"); //可以播放,但中途可能因为加载而暂停<br /> eventTester("canplaythrough"); //可以播放,歌曲全部加载完毕<br /> eventTester("seeking"); //寻找中<br /> eventTester("seeked"); //寻找完毕<br /> eventTester("timeupdate"); //播放时间改变<br /> eventTester("ended"); //播放结束<br /> eventTester("ratechange"); //播放速率改变<br /> eventTester("durationchange"); //资源长度改变<br /> eventTester("volumechange"); //音量改变<br /> }<br /> 小伙伴们主要看下本文的思路即可,希望对大家能有所帮助