简体   繁体   English

静音和取消静音按钮切换html5音频

[英]mute and unmute button toggle html5 audio

I've understood how to mute an <audio> sound object by putting this code : 我已经了解了如何通过放置此代码来静音<audio>声音对象:

<a href="#" onclick="document.getElementById('ambiance').muted = true; return false">
    mute sound
</a>

Now I'm searching how to mute/unmute my sound using the same button with the option to toggle it ? 现在我正在搜索如何使用相同的按钮来选择切换声音来静音/取消静音?

Can anyone give me directions? 谁能给我指示?

var audioElm = document.getElementById('ambiance'); audioElm.muted = !audioElm.muted;

Try this: 尝试这个:

 .unmute { background-image: url(http://franriavilla.in/images/unmute.png); background-size: cover; width: 35px; height: 30px; cursor: pointer; } .mute { background-image: url(http://franriavilla.in/images/mute.png); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <audio id="track" autoplay="autoplay" loop="loop"> <source src="audio/ThemePackNet.mp3" type="audio/ogg" /> </audio> <div class='unmute' onclick="document.getElementById('track').muted = !document.getElementById('track').muted;$(this).toggleClass('mute')"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM