在标记检测上播放音频的恒定警报

时间:2018-04-30 16:16:49

标签: javascript jquery aframe ar.js

我有一个有A-frame和AR.JS的网站,我可以在标记检测时播放音频。但是,当标记在视图中时,我会在控制台中不断弹出此警报。

'components audio warn All the sounds are playing. If you need to play more sounds simultaneously consider increasing the size of pool with the poolSize attribute. <a-entity sound=​"src:​ #sound" autoplay=​"false">​​'

似乎要求每一帧/几帧。关于如何阻止这个的任何想法?这似乎是不好的行为,特别是对于移动设备。

供参考,这是场景代码:

<a-scene embedded arjs='sourceType: webcam; debugUIEnabled: false;';>
       <a-assets>
            <audio id="sound" src="audio.mp3" preload="auto"></audio>
        </a-assets>
        <a-marker preset="custom" type="pattern" url="img/pattern-marker.patt">
        <!--<a-marker preset="hiro">-->
        <!--<a-torus-knot color="#000000" arc="180" p="2" q="7" radius="5" radius-tubular="0.1"></a-torus-knot>-->
                <a-box position='0 0.5 0' material='color: black;' soundhandler> 
                </a-box>
        </a-marker>
        <a-entity sound="src: #sound" autoplay="false"></a-entity>
        <a-entity camera></a-entity>
</a-scene>

以及注册组件和事件的代码:

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

     }
});

2 个答案:

答案 0 :(得分:1)

取决于你的目标:

1)您可以在播放声音之前通过检查currentTime属性来检查HTML媒体元素是否正在播放。或者设置任何响应playingended音频元素事件的布尔变量。

2)只要标记不可见,您就可以pause()音频。

您可以查看here,只有在音频结束/重新加载时,音频才会播放。 更多关于媒体here

答案 1 :(得分:1)

您可以使用标志变量并订阅声音事件:

AFRAME.registerComponent('soundhandler', {
    playingSound = false, //flag vble.
    soundToHandle = document.querySelector('[sound]'),
    init: function() {
      soundToHandle.stopSound = function() {  //sound stoped event
        playingSound = false;
      };
      soundToHandle.playSound = function() { //sound played event
        playingSound = true;
      };
    },
    tick: function () {
         if ((document.querySelector('a-marker').object3D.visible == true) && (!playingSound)) {
            soundToHandle.components.sound.playSound();
        } else if ((document.querySelector('a-marker').object3D.visible == false) && (playingSound)){
             soundToHandle.components.sound.stopSound();
        }

     }
});
相关问题