Play and pause audio in AR

I have play on awake enabled in my audio but right now in Android, it continues to play the audio in XRSession without pausing itself. I want to pause the audio when we enter the XRSession till the object is placed then play once the model is placed in XR. Is there any callback called when the model is placed, which I can use, or any other workaround to achieve this? Thanks in advance

Hi Ashok, in order to achieve the desired functionality of pausing the audio when entering an XR session and resuming it after the model is placed, you can utilize XRSession events along with the audio control methods provided by Needle Engine.

Here’s the step-by-step plan:

  1. Detect XR Session: Use the onSessionStart event to pause the audio when the XR session starts.
  2. Model Placement: Implement a callback or a listener when the model is placed in the XR session.
  3. Resume Audio: Play the audio once the model is successfully placed.

Here’s an example code snippet to illustrate this:

import { XRSession, AudioComponent } from "@needle-tools/engine";

// Assuming you have a reference to your audio component
const audioComponent = new AudioComponent();

// Function to handle entering XR session
function onXRSessionStart(session) {
    audioComponent.pause(); // Pause the audio
}

// Function to handle when the model is placed
function onModelPlaced() {
    audioComponent.play(); // Resume audio after model is placed
}

// Event listeners for XR session
XRSession.addEventListener('start', (session) => {
    onXRSessionStart(session);
});

// Add your model placement logic and call onModelPlaced when done
// For example (pseudo-code):
placeModelInXR().then(() => {
    onModelPlaced();
});

Key Points:

  • Pause the audio when the XR session starts.
  • Resume the audio once the model placement process is complete.
  • Ensure that your audio component is properly referenced and that the play and pause methods are accessible.

If you need more help or specific details on any API methods, feel free to ask!

1 replies left - Get a PRO license for more AI replies.
thread_YNksZGxBBUcnGIohIzmJvytU
Reply to this message to continue the conversation

I just want to know if any default callback is triggered when the model is placed or if any other way to achieve this.

I’m sorry, I couldn’t find a response. :cactus:

Hello, you can use the static WebARSessionRoot.onPlaced(yourcallback) method to receive an event when the scene has been placed and use e.g. the onEnterXR() component method for receiving an event in your component when AR starts

// e.g. in global context or in a component
WebARSessionRoot.onPlaced(evt => { 
  // Handle audio
});

Thanks for the reply. I also have another issue. My scene has only one animation which is around 60 seconds (just a simple unity animation where go scaled up and down). It keeps crashing on Quicklook without any errors in the console, it just shows Universal Scene Description Package {package_size}. Is there any limitation for animation length?

Hi, there’s no such limit - I’ve forwarded your report and we’ll get back to you!

Thanks

@ashok_99 could you try your scene with the latest release? Releases & Changelogs - #41 by needle-bot

Will try and update. Thanks


The issue still there

Could you report a bug with your scene via the menu item Needle Engine/Report a Bug ?
Thank you

Just uploaded a bug report, Hope it gets resolved soon. Thanks