HTML5音频停止功能 [英] HTML5 Audio stop function

查看:97
本文介绍了HTML5音频停止功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我点击我的导航中的每个链接播放一个小音频片段

HTML代码:

 < audio tabindex =0id =beep-onecontrols preload =auto> 
< source src =audio / Output 1-2.mp3>
< source src =audio / Output 1-2.ogg>
< / audio>

JS code:

  $('#links a')。click(function(e){
e.preventDefault();
var beepOne = $(#beep-one)[0 ];
beepOne.play();
});

到目前为止工作情况良好。



问题是声音片段已在运行,并且我点击任何链接时都不会发生任何事情。 我试图阻止点击时已播放的声音链接,但在HTML5的音频API中没有直接事件



我尝试了以下代码,但它无法正常工作

  $。each($('audio'),function(){
$(this).stop();
});

有什么建议吗?

解决方案

而不是 stop()你可以尝试:

  sound.pause(); 
sound.currentTime = 0;

这应该会产生预期的效果


I am playing a small audio clip on click of each link in my navigation

HTML Code:

<audio tabindex="0" id="beep-one" controls preload="auto" >
    <source src="audio/Output 1-2.mp3">
    <source src="audio/Output 1-2.ogg">
</audio>

JS code:

$('#links a').click(function(e) {
    e.preventDefault();
    var beepOne = $("#beep-one")[0];
    beepOne.play();
});

It's working fine so far.

Issue is when a sound clip is already running and i click on any link nothing happens.

I tried to stop the already playing sound on click of link, but there is no direct event for that in HTML5's Audio API

I tried following code but it's not working

$.each($('audio'), function () {
    $(this).stop();
});

Any suggestions please?

解决方案

instead of stop() you could try with:

sound.pause();
sound.currentTime = 0;

this should have the desired effect

这篇关于HTML5音频停止功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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