在一个帧中,当新声音开始播放时,如何停止所有声音? [英] In a-frame how can I stop all sounds when a new sound starts playing?
问题描述
当我单击a实体时,它会播放声音,但是在播放时如何使它停止所有其他声音,所以这不是一团混乱的东西吗?
When I play click on the a-entity it plays the sound, but how can I get it to stop all other sounds when played, so it is not a loud muddled mess of a thing?
我已经搜索了这个问题并尝试添加他们使用的代码,但是它们没有起作用,我已经尝试了大约5种不同的代码。
I've googled the problem and tried adding the codes they use, but they haven't been working, I've tried about 5 different ones.
我正在像这样运行声音:
I am running my sound like this:
<audio id="mercury-sound" src="mercury.mp3" preload="auto"></audio>
<script id="mercury" type="text/html">
<a-entity class="mercury"
geometry="primitive: sphere; radius: 0.67"
material="shader: flat; src: ${thumb}"
event-set__mouseenter="_target: #image-mercury; material.src: ${src}; opacity: 1"
event-set__mouseleave="_target: #image-mercury; material.src: ${src}; opacity: 0">
</a-entity>
</script>
<a-entity template="src: #mercury" sound="src: #mercury-sound; on"></a-entity>
我希望它播放声音并在播放时停止所有其他声音。
I want it to play the sound and stop all other sounds when played.
编辑:如果其他任何人遇到此问题,则此问题已解决
if anyone else is having this issue, this is what fixed it
模板组件为创建子节点,您需要抓取el = e.target.parentNode。在这里也检查一下,我将在js中管理所有与声音相关的逻辑,但这就是另一个主题:) btw哪里的冥王星不好! – Piotr Adam Milewski
The template component is creating child nodes, You need to grab let el = e.target.parentNode. Check it out here also i would manage all sound-related logic in js, but thats another topic :) btw where is poor pluto ! – Piotr Adam Milewski
推荐答案
这是使用 js的方法
。这个想法是:
Here's an approach using js
. The idea is to:
1)保留带有声音的元素数组
2)单击一个元素-遍历该数组并停止声音
3)播放被点击的
1) keep an array of the elements with sound
2) when one is clicked - iterate through the array and stop sounds
3) play the 'clicked' one
具有这样的设置:
<a-box sound='#first'></a-box>
<a-box sound='#second'></a-box>
<a-box sound='#third'></a-box>
您可以创建一个脚本,该脚本将:
You can create a script, which will:
// grab all elements with the sound component
var soundEls = document.querySelectorAll('[sound]')
// make sure all of those react on a 'click' event
for (var i = 0; i < soundEls.length; i++) {
var item = soundEls[i];
item.addEventListener('click', e => {
let el = e.target
// don't replay an already playing sound
let isPlaying = el.components.sound.isPlaying
// stop all sounds
soundEls.forEach(soundEl => {
soundEl.components.sound.stopSound()
})
// play the clicked sound if isn't already playing
if (!isPlaying)
el.components.sound.playSound()
})
}
通常,我建议使用aframe 自定义组件。但是,以这种方式获得创意可能会更容易。在此故障中进行检查。
Usually, i'd recommend using an aframe custom component. But it may be easier to get the idea this way. Check it out in this glitch.
这篇关于在一个帧中,当新声音开始播放时,如何停止所有声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!