【发布时间】:2023-04-01 21:32:01
【问题描述】:
我有一些元素可以在单击或鼠标输入时播放声音。我想在用户单击按钮时将这些声音效果静音,并在再次单击按钮时取消静音。
示例: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;
});
}
});
【问题讨论】:
标签: javascript aframe