utopia-rise / fmod-gdextension

FMOD Studio GDExtension bindings for the Godot game engine
MIT License
439 stars 48 forks source link

Music plays at background in Android - Godot 3.5 #139

Closed KaanSali closed 2 years ago

KaanSali commented 2 years ago

Firstly I want to thank you for integrating and supporting FMOD on Godot.

I have properly setup and use Fmod with Windows and Android. But at Android, when I press physical button HOME, the music loop continues at background. When I close the application, it stops.

I tried to use notifications with pause_all_events method but it stops after I reopen the application. I also tried to pause with set_event_paused but it didn't work too.

In Windows, when I use set_pause_mode(2) in Fmod _ready function, it works perfectly fine. My codes are at below.

func OnFocusOut(): Fmod.pause_all_events(true)

for eventId in EventInstances:

#   Fmod.set_event_paused(EventInstances[eventId],true)

func _notification(what): match what: MainLoop.NOTIFICATION_WM_FOCUS_OUT: OnFocusOut() MainLoop.NOTIFICATION_WM_FOCUS_IN: OnFocusIn() MainLoop.NOTIFICATION_APP_PAUSED: OnFocusOut() Fmod.NOTIFICATION_PAUSED: OnFocusOut()

CedNaru commented 2 years ago

I might understand what the issue here is. It's related to how FMOD works, every time you call an FMOD method, it's not immediately executed. Instead, the commands are batched and only run when you call the Fmod update function in the C++ code. That function usually is automatically called by the FMOD singleton in the _process method.

Basically, FMOD is updated at the same time Godot is. Now the issue is when you tap home on android, you likely have the pause_all_events() called, but update() is never called because Godot itself just paused. It's probably the reason why the events stop only when you reopen the application because only at this moment your command is run.

To fix that I think you have to make sure Fmod._process() is called AFTER you call Fmod.pause_all_events().

KaanSali commented 2 years ago

Thank you very much, I added Fmod.process(delta) in my OnFocusOut function and problem is solved.

func OnFocusOut(): Fmod.pause_all_events(true) Fmod._process(_delta)