简体   繁体   English

需要帮助静音多个音频文件

[英]Need help muting multiple audio files

I'm helping a student with their work, and we have a mute button working using IDs, but of course that only mutes one thing at a time.我正在帮助一名学生完成他们的工作,我们有一个使用 ID 的静音按钮,但当然一次只能静音一件事。 We want to be able to shut off ALL audio playing at once.我们希望能够一次关闭所有音频播放。 If I try to use anything other than getElementById in my script, it just breaks and doesn't mute at all.如果我尝试在我的脚本中使用 getElementById 以外的任何内容,它只会中断并且根本不会静音。 For example, using "getElementbyClassName("soundsnip")" doesn't mute anything.例如,使用 "getElementbyClassName("soundsnip")" 不会使任何内容静音。 The only thing successfully working is muting one thing only with "getElementById".唯一成功的事情就是只用“getElementById”静音一件事。 Can anyone help us be able to mute all of these elements with one button?谁能帮助我们一键静音所有这些元素?

Here's the working code we have now, muting only one element:这是我们现在的工作代码,仅静音一个元素:

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">+</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#">white noise 
        <br>
        <br>
    <audio loop controls class="soundsnip" id="white1">
  <source src="whitenoise.wav" type="audio/wav"></audio></a>
    <a href="#">mic rubbing
     <br>
        <br>
    <audio loop controls class="soundsnip" id="mic1">
  <source src="micrub.wav" type="audio/wav"></audio></a>
    <a href="#">mic scratching
     <br>
        <br>
    <audio loop controls class="soundsnip" id="scratch1">
  <source src="micscratch.wav" type="audio/wav"></audio></a>
  </div>
</div>

<button onclick="enableMute()" type="button">Mute sound</button>
<button onclick="disableMute()" type="button">Enable sound</button>

<script>
var aud = document.getElementById("white1");
function enableMute() { 
  aud.muted = true;
} 

function disableMute() { 
  aud.muted = false;
} 

function checkMute() { 
  alert(aud.muted);
} 
</script>

First, select all the audio elements, with document.querySelectorAll() .首先,使用document.querySelectorAll()选择所有音频元素。 Then, loop over them.然后,循环它们。 Try something like this.尝试这样的事情。

for (audioEl of document.querySelectorAll('audio')) {
  audioEl.muted = true;
}

As an alternative, you could connect these to a Web Audio API graph and use a GainNode.作为替代方案,您可以将这些连接到 Web Audio API 图并使用 GainNode。

Here's working example, it will mute all audio on button click:这是工作示例,它将在单击按钮时使所有音频静音:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">+</button> <div id="myDropdown" class="dropdown-content"> <a href="#">white noise <br> <br> <audio loop controls class="soundsnip" id="white1"> <source src="https://www.milannohejl.cz/subdom/codepen/Shantifax-WeAreAllOne.mp3" type="audio/wav"></audio></a> <a href="#">mic rubbing <br> <br> <audio loop controls class="soundsnip" id="mic1"> <source src="https://www.milannohejl.cz/subdom/codepen/Shantifax-WeAreAllOne.mp3" type="audio/wav"></audio></a> <a href="#">mic scratching <br> <br> <audio loop controls class="soundsnip" id="scratch1"> <source src="https://www.milannohejl.cz/subdom/codepen/Shantifax-WeAreAllOne.mp3" type="audio/wav"></audio></a> </div> </div> <button type="button" id="mute-button">Mute sound</button> <button type="button" id="unmute-button">Enable sound</button> <script> $(document).ready(function() { /*** Mute all ***/ $('#mute-button').on('click', function() { /*** Mute all video and audio on page ***/ $('body video, body audio').each(function() { /*** Do it here globally ***/ $(this).prop('muted', true); }); }); /*** UnMute all ***/ $('#unmute-button').on('click', function() { /*** Un Mute all video and audio on page ***/ $('body video, body audio').each(function() { /*** Do it here globally ***/ $(this).prop('muted', false); }); }); }); </script>

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

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