Closed peterlunglum closed 6 years ago
See https://github.com/jeromeetienne/AR.js/issues/217 and https://github.com/jeromeetienne/AR.js/pull/303. The markerFound
event does not exist in AR.js yet — until some sort of event is made available in the AR.js library, you'll likely need to compile a custom version of the library or check visibility and emit events yourself unfortunately.
Hi @peterlunglum As explained in this post, you can register a component in aframe that checks if the marker is visible...
Here is the code to put in your header `
//To avoid playing from start if the marker flickers, not sure if needed, but it might be a failsafe when you have mutiple markers and want to make sure the first finishes before launching the second
var playing = false;
//HTML5 audio, will need user touch input to start on mobile
var intro = new Audio("sound.mp3");
//Detect end of audio
intro.addEventListener("ended", function() {
intro.currentTime = 0;
playing = false;
});
AFRAME.registerComponent('markerhandler', {
init: function() {
// Set up the tick throttling. Will check if marker is active every 500ms
this.tick = AFRAME.utils.throttleTick(this.tick, 500, this);
},
tick: function(t, dt) {
if (document.querySelector("#your_marker_id").object3D.visible == true && playing == false) {
// MARKER IS PRESENT
intro.play();
playing = true;
} else {
// MARKER IS HIDDEN, do nothing (up to you)
}
}
});
`
And of course, add the id to your a-marker
Hope this helps :-)
"adding a id in the a-marker"? what do you mean?
Inside the tick, there is an if clause that checks if the object with id #your_marker_id is visible. That's the id you should put in your a-marker (you can change it, of course)
Good luck ;-)
yeah, i get it.
i asked cuz i never saw an id inside the a-marker tag. i tried put an id in the a-marker tag, but didn't see to work.
if (document.querySelector("#markerid").object3D.visible == true) {
<a-marker-camera preset='hiro' registerevents emitevents="true" id="markerid"></a-marker-camera>
@vettorazi try with this properties on the element (and maybe try also a-marker instead of a-marker-camera if the first doesn't work):
<a-marker markerhandler cursor="rayOrigin: mouse" emitevents="true" id="animated-marker" preset='hiro'></a-marker>
I collect what i learned about events on ar.js in this article. It contains a full working example for click events. Hope it helps!
https://medium.com/chialab-open-source/how-to-handle-click-events-on-ar-js-58fcacb77c4
@peterlunglum @vettorazi if this is solved / obsolete I will close, let us know please
@nicolocarpignoli you mention emitevents="true"
in a previous comment, but I thought the PR for that (https://github.com/jeromeetienne/AR.js/pull/303) had not been merged? I think the problem here is not that users don't know how to listen for events, but that there is no event available at all when a marker is found or lost.
@nicolocarpignoli I am going to play video when marker is detected. I am new to AR.js and trying with a lot of examples but none of them are working. Can you help me?
@delphinoluiz
see this repo. on index.html you have the elements definition and on events.js you have your eventListener for click event. When a user clicks on the 3D object that is shown on top of the marker, we can do something.
Now, if you want to play an audio track, simply call 'play()' method of HTML5 audio elements https://www.w3schools.com/jsref/met_audio_play.asp
In the example I provide, on events.js, I change the scale of the 3D object. Instead, you could play an audio:
// instead of this:
const scale = aEntity.getAttribute('scale');
Object.keys(scale).forEach((key) => scale[key] = scale[key] + 1);
aEntity.setAttribute('scale', scale);
// do something like this:
const audio = document.querySelector('audio');
audio.play();
Of course, before that, you have to attach an audio
element on the index.html with the src
attribute that points to a valid audio source.
:)
@nicolocarpignoli Thank you very much. I want to play "video" using 'a-video'. Can you help me? Do you have any repo for video play?
@delphinoluiz never used it sorry :) you can check here
@nicolocarpignoli Thanks
you're welcome @delphinoluiz
how to play audios for marker ,when you have 6 audio files for 6 respective markers in webAR using AFRAME and AR.js.
I am trying to create a web application where I want different audios to be played on detecting different markers.(Each marker has been assigned an audio file.only one marker will be shown at a time.
I had create eventlistners for all 6 files as follows
`
the audio asset was added as follows:
<audio
src="https://arjs-cors-proxy.herokuapp.com/https://raw.githack.com/Shabeera185/BookAR/main/audioBEE.mp3"
preload="auto"
id="sid2"
response-type="arraybuffer"
loop
autoplay
>`
and the audio has been placed under<a-audio src="#sid2"> </a-audio>
when the programme is run, whichever audiohandler is put at last in Githubissues.
How would I get an event listener to work with marker detection?
I've looked around and found a fork for this in an unmerged repo, but to use the main repo, looks like I would have to registerComponent?
I have the following added in a script tag after Aframe and AR.JS are called on, but placed before the a-scene.
Here's the scene:
From registering the component though, it doesn't seem to even console.log when the marker is detected. Any ideas on why?