Play audio once when target is found A-frame & Ar.js

84 views Asked by At

I want a sound to start playing once a specific target was found. However the way I tried, it I just can't get it to play without autoplay. I'm very new to this so any help is highly appreciated :)

<script>
    document.addEventListener("DOMContentLoaded", function() {

        const sound = document.querySelector('#sound');
        // detect target found
        sound.addEventListener("targetFound", event => {
        var sound = document.querySelector('[sound]');
        sound.components.sound.playSound();
        });

    });
 </script>
<a-assets>
    <audio id="sound" src="mp3/sound.mp3" preload="auto"></audio>
</a-assets>


<a-entity id="sound" mindar-image-target="targetIndex: 1">
    <a-sound src="#sound" loop="false" autoplay="false" poolSize="1"></a-sound>
</a-entity>
0

There are 0 answers