NiklasEi / bevy_game_template

Template for a Bevy game including CI/CD for web, Windows, Linux, macOS, iOS and Android
https://niklasei.github.io/bevy_game_template/
Creative Commons Zero v1.0 Universal
844 stars 89 forks source link

sound.js: all sound are played when getting focus #120

Closed mirsella closed 3 months ago

mirsella commented 3 months ago

hello, im new to bevy

when the even listener trigger it call the function to resume all audio context, but if there is a lot of audio context or older one since the start of the page when it didn't had focus, they all play at the same time.

is there a way of to not resume previously played audiocontext, and just make the new one work ? like ignore the previous one that were blocked.

thanks !

NiklasEi commented 3 months ago

Hey, you can probably play around with the script and see if you can get to behave as you want.

I think the current behaviour is a good default.

mirsella commented 3 months ago

thanks for the answer! I've tried setting the gain to 0 to mute it but didn't manage to make it work. would you see another way of doing this or setting the volume to 0 then putting it back should work ?

NiklasEi commented 3 months ago

If I understand your original question correctly, you can just remove the script. Without the script, contexts started after a user interaction will play audio and previously started ones will be ignored.

mirsella commented 3 months ago

that's what I thought too. using the demo breakout game, even after focusing the page, no audio is played. I guess it use the same audiocontext for every collision, instead of creating new one, and since it's not .resume(), even new sounds are not played. the demo breakout spawn a new audiobundle, I would though it would be different audio context, yes. maybe it's the same because it's the same asset ? https://github.com/bevyengine/bevy/blob/main/examples/games/breakout.rs

mirsella commented 3 months ago

oh maybe since the audiobundle doesn't finish playing, it's not despawned, and so doesn't re create a new audiocontext after ? I don't have enough knowledge in bevy for that, I'll try when I got time

NiklasEi commented 3 months ago

I think the audio context is just started once from the audio backend. It has nothing to do with the actual sounds playing.

A solution to your issue might be to keep track of the first user interaction in your game. Then you can not play audio before that interaction and the context resume should work fine.

mirsella commented 3 months ago

thanks for the idea, I'll insert a ressource on the first Input/Mouse/Touch event and check if it exist when playing sound