繁体   English   中英

A-Frame 静音所有声音,包括按钮单击时的声音分量

[英]A-Frame mute all sound including sound component on button click

我有一些元素可以在单击或鼠标输入时播放声音。 我想在用户单击按钮时使这些声音效果静音,并在再次单击该按钮时取消静音。

示例: https : //pebble-kiss.glitch.me/

到目前为止,我只能将背景音频静音,而不能将鼠标输入和单击音频静音。 有没有办法做到这一点,还是我必须在 js 中管理所有声音?

 <a-plane id="audioButton" color="#FF0000" width=".5" height=".5" position="-2 2 0" audiohandler></a-plane>

 <a-box class="sound" sound="src: #audio; autoplay: true; loop: true; volume: 15;" position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9" shadow></a-box>

 <a-box class="sound" position="1 0.5 -3" rotation="0 45 0" color="#000000" sound="on: mouseenter; src: #mouseenter;"></a-box>

 <a-box class="sound" position="2.5 0.5 -3" rotation="0 45 0" color="#00FF00" sound="on: click; src: #click;"></a-box>





    AFRAME.registerComponent('audiohandler', {
    init:function() {
      var playing = true;
      var audio = document.querySelectorAll(".sound");
      this.el.addEventListener('click', function() {
      console.log("click");
        if(!playing) {
          audio.forEach(function(playAudio) {
            playAudio.components.sound.playSound();
          });
        } else {
          audio.forEach(function(pauseAudio) {
            pauseAudio.components.sound.stopSound();
          });
        }
        playing = !playing;
      });
    }
  });

您的组件正在停止和播放声音之间切换。 如果您想将它们静音,只需调高音量即可。

由于每个元素都有不同的体积,因此您应该存储它们。 在尝试抓取卷之前,请确保已加载元素!

当您想静音时,只需遍历元素,并在 0(静音)和存储值之间切换:

AFRAME.registerComponent('muter', {
  init:function() {
    var audio = document.querySelectorAll(".sound");
    // lets store volume levels for later use
    var volLevels = {}
    audio.forEach(function(el, index) {
      el.addEventListener('loaded', e=> {
        volLevels[index] = el.getAttribute('sound').volume
      })
    })

    var muted = false
    // when clicked - switch the volume
    this.el.addEventListener('click', function() {
      audio.forEach(function(playAudio, index) {
        let volume = muted ? volLevels[index] : 0
        playAudio.setAttribute('sound', 'volume', volume)
      });
      muted = !muted
  });
 }
});

故障在这里

暂无
暂无

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

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