Hi everyone,
I’m using Babylon.js inside a React application, and I want to properly use the AudioEngine—especially considering browser autoplay restrictions and the fact that audioEngine.unlockAsync() must be called on a user interaction.
Here are some details:
-
I initialize the Babylon scene inside a
useEffect, and I want to create sounds (some should play only after collisions or triggers). -
However, calling
CreateAudioEngineAsync()and usingunlockAsync()causes issues if it’s not done on a user gesture (likeonClick). -
I also noticed that sometimes the audio won’t play or throws an error unless the engine is unlocked.
What is the best practice or pattern to:
-
Set up the
AudioEnginein a React-friendly way (maybe via context or hook)? -
Ensure
unlockAsync()is only triggered after a user gesture (and how to detect that)? -
Play certain sounds later (e.g., after collision), even if they are initialized earlier?
Here’s a simplified version of what I tried:
const initScene = async () => {
const audioEngine = await CreateAudioEngineAsync();
await audioEngine.unlockAsync(); // 🔴 Causes issue if not in user interaction
};
Any example patterns, custom hooks, or tips would be really appreciated!
Thanks ![]()