mute and unmute button toggle html5 audio

▼魔方 西西 提交于 2019-11-27 07:17:09

问题


I've understood how to mute an <audio> sound object by putting this code :

<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?


回答1:


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



回答2:


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>


来源:https://stackoverflow.com/questions/7798530/mute-and-unmute-button-toggle-html5-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!