微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

用户收听音频元素超过5秒钟时,我想记录.如何用 HTML5检测?

解决方法

我将处理播放事件,然后在完成后使用setTimeout调用轨道.这样的东西(伪代码):
var timeHandler = null;

// if they stop listening,don't give them the alert
   myAudioElement.addEventListener('stop',function() {
     if (timeHandler) clearTimeout(timeHandle);
   });

// when they start to play,set an event to pop up 5 seconds later
    myAudioElement.addEventListener('play',function() {
        timeHandler = setTimeout(5000,function() {
          // here I would make some kind of ajax call to log the event
          alert("it's been 5 seconds!");
        });

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

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

祝你好运!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。