在标记检测 A 帧和 Ar.js 上播放音频一次

问题描述

当使用 A-frame 和 AR.JS 库检测到标记时,我尝试只播放一次声音。

我正在尝试下面的代码行,但声音一直在播放。

<a-scene embedded arjs='sourceType: webcam; debugUIEnabled: false;';>
     <a-assets>
         <audio id="sound" src="audio.mp3" preload="auto"></audio>
     </a-assets>
     <a-marker preset="hiro">
                <a-entity id="examplemodel" gltf-model="./models/Example.glb" soundhandler></a-entity>
     </a-marker>
     <a-entity sound="src: #sound" autoplay="false"></a-entity>
     <a-entity camera></a-entity>
</a-scene>

<script>
AFRAME.registerComponent('soundhandler',{
    tick: function () {
         var entity = document.querySelector('[sound]');
         if (document.querySelector('a-marker').object3D.visible == true) {
             entity.components.sound.playSound();
         } else {
             entity.components.sound.pauseSound();
         }
    }
});
</script>

当我更改 init 的刻度时,出现此错误

未捕获的类型错误:无法读取未定义的属性“playSound”

你能给我一些解决这个问题的想法吗?

解决方法

它无限期地播放,因为一旦它可见 - 在每个渲染循环中你调用 playSound()

如果您添加一个简单的切换检查 - 您将获得“每个可见一次”的结果:

tick: function() {
  // marker is the object3D of marker
  if (marker.visible && !this.markervisible) {
    // do your stuff here once per visible
    this.markervisible = true;
  } else if (!marker.visible) {
    this.markervisible = false;
  }
}

检查一下here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...