Play sound on click in A-Frame

一笑奈何 提交于 2019-11-28 10:05:51

问题


So I'm struggling to find a solution to play/stop/pause sound on "click" i.e. when focusing with a black dot with A-Frame sound entity... What I would like to have is a plain, shape or whatever with a play/pause image on it, which would trigger audio when focused. Did anyone encounter something similar perhaps?

<audio id="sound" crossorigin="anonymous" preload="auto" src="some-audio-file.mp3"></audio>

... would trigger something like sound="on: click; src: #sound"

回答1:


try making a custom component

AFRAME.registerComponent('audiohandler', {
  init:function() {
     let playing = false;
     let audio = document.querySelector("#audio");
     this.el.addEventListener('click', () => {
          if(!playing) {
              audio.play();
           } else {
              audio.pause();
              audio.currentTime = 0;
           }
           playing = !playing;
     });
  }
})

and use it within Your 'button"

<a-box audiohandler> </a-box>

You can check all media methods, properties etc here.
You can check this button here.



来源:https://stackoverflow.com/questions/47921013/play-sound-on-click-in-a-frame

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