How to properly trigger sounds to play dynamically (on a separate function)?

Hey there!
I’m loading a sound using the AssetsManager (as per this doc page - Audio | Babylon.js Documentation ).

However, in this case they’re running the ‘sound.play()’ in a function that runs within the asset ‘loading’ function instantiation. I wanted to be able to use the ‘.play()’ function in a different place to be more flexible but I’m not getting any sound playback.

Even doing it as explained in this video - Sound FX with 2 Lines of Code! - YouTube the sound never plays.
What’s the proper way to play sound when not in the callback of the ‘new BABYLON.Sound()’ instantiation?

How would one make it so that, as an example, a sound would be played whenever there’s a physics collision detected?
I’m not sure I’m understanding how this is supposed to work, I find it weird that the file/data has to be loaded every time the sound needs to be played…
Any suggestions would be much appreciated.

Hi VisionsMind,

Does this Playground do what you’re trying to do? And if so, does it work?

Sounds not ready to play | Babylon.js Playground (babylonjs.com)

That runs as I expect it to in my browser (Chromium-based Edge on Windows 10), but this might be a case of usage scenario issues as well. I’ve run across weird browser audio issues in the past, so I think audio might just be one of those arenas where browsers can sometimes behave a little strangely. If the Playground above isn’t relevant to your issue, can you please link to a Playground of your own that shows the scenario that isn’t working?

2 Likes

Thank you so much for your reply and for the suggestion!
It could be a solution for some cases! However, I’m not sure that would be the best approach for a more dynamic situation like the one in the example I gave - play a sound for every collision that gets registered. Also, the amount of time to wait seems totally arbitrary if hard-coded that way…I’m not too sure…

Okay, I think I understand now. The point of the setTimeout was just to use the variable somewhere that was noticeably distinct from the callback to demonstrate that it would still play a sound even when used elsewhere/later. If that works in your scenario – that is, if you don’t hear the sound play immediate, but you do after five seconds – then you should be able to use that variable in exactly the same way wherever/whenever you want to. The setTimeout doesn’t actually matter; as long as you have access to that variable, you should be able to use it where and when you need to. Hope this helps, and best of luck!

1 Like

Yeah, it’s a nice tip! It’s just that I was looking for a solution that would cater to both - the sound object instance to be available somewhere distinct from the callback and be able to trigger the playback with some level of ‘precision’, if that makes sense.

I’ll take note of this one for sure! Might come in handy! Thank you!

@VisionsMind, the audio AssetsManager docs page you referenced provides this PG https://playground.babylonjs.com/#PCY1J#18 which sets up the flag soundsready once all the assets are loaded. Checking soundsReady === 3 will allow you to play any of the pieces wherever you want, eg https://playground.babylonjs.com/#PCY1J#168

1 Like

Hey there! Sorry for the delay.
Thanks for the playground! I’ll give this solution a try.

1 Like

Does the sound need to be run on the observable to be played?
How could I make something like this - https://playground.babylonjs.com/#UTCS4Q#1
work?
Just having a function that plays the sounds when called is not possible?