[Web] Audio not muting on tab change / document.visibilitychange not detected (Godot 4.3)

Godot Version

4.3.stable.official

Question

Hi everyone!

I’m exporting a game with Godot 4.3 (Web) and I want to automatically mute all audio when the browser tab is switched or minimized.

I’ve tried using _notification(NOTIFICATION_WM_FOCUS_OUT) and DisplayServer.window_has_focus(), but they don’t work in the web export.

I also added a visibilitychange listener using JavaScriptBridge.eval(), but couldn’t get it to properly call a GDScript function.

Could someone please share a reliable method or example of how to detect document.visibilitychange in a Godot Web export and call a GDScript function (e.g. mute_audio())?

I’d really appreciate any help! :folded_hands:

This is no longer a thing,

Try NOTIFICATION_APPLICATION_FOCUS_OUT

A second option is to set your own event callback in the display server. window_set_window_event_callback and listen for WINDOW_EVENT_FOCUS_OUT.

But doing some googling for browser events this may not be a widley supported feature on web. I tried looking at the engine but didnt see anything special, but it looks like it might try and forward focus and mouse enter events from the java script wrapper.