如何检测HTML5音频标签是否已被收听至少x秒?

前端之家收集整理的这篇文章主要介绍了如何检测HTML5音频标签是否已被收听至少x秒?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
用户收听音频元素超过5秒钟时,我想记录.如何用 HTML5检测?

解决方法

我将处理播放事件,然后在完成后使用setTimeout调用轨道.这样的东西(伪代码):
  1. var timeHandler = null;
  2.  
  3. // if they stop listening,don't give them the alert
  4. myAudioElement.addEventListener('stop',function() {
  5. if (timeHandler) clearTimeout(timeHandle);
  6. });
  7.  
  8. // when they start to play,set an event to pop up 5 seconds later
  9. myAudioElement.addEventListener('play',function() {
  10. timeHandler = setTimeout(5000,function() {
  11. // here I would make some kind of ajax call to log the event
  12. alert("it's been 5 seconds!");
  13. });

一如以往,根据您的需要,这可能会变得更加复杂.您也可以使用ontimeupdate事件来跟踪播放头目前所在的位置.有关html5音频事件的参考,请查看此页面

https://developer.mozilla.org/en/DOM/Media_events

祝你好运!

猜你在找的HTML5相关文章