HTML音频 - 使用jQuery检测位置 [英] HTML Audio - Detecting position with jQuery

查看:132
本文介绍了HTML音频 - 使用jQuery检测位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用HTML5音频标签来播放eventListener或使用Dom / jQuery的动作在播放或结束时触发事件?

Is there anyway when using the HTML5 Audio tag to have an eventListener or action using Dom/jQuery to fire an event during playback or at the end?

这个答案链接到currentTime,但没有例子,下面是伪代码。

This answer links to currentTime but there are no examples, Pseudo code below.

<!DOCTYPE html>
<html>
<body>

<audio controls="controls">
  <source src="file.ogg" type="audio/ogg" />
  <source src="file.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>

<script src="jquery.js"></script>
<script>
$(document).ready(function () {
    $("audio").addEventListener("end", function () {
        alert("song has ended");
    });
    $("audio").addEventListener('20.2', function () {
        alert("your currently at 20.2 seconds in the track");
    });
});
</script>

</body>
</html>


推荐答案

你可以试试这个:

获取音频的持续时间,并根据当前时间和显示警报进行警报,无论您需要什么。

Get the duration of the audio and make an alert based on the currentTime and display alert wherever you require.

audio.addEventListener("timeupdate", function(){    
var duration = document.getElementById('duration');
var s = parseInt(audio.currentTime % 60);    var m = parseInt((audio.currentTime / 60) % 60);
duration.innerHTML = m + '.' + s + 'sec'; 
}, false);

这篇关于HTML音频 - 使用jQuery检测位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆