Pause music when the game tab of browser is no longer enabled

Hello!

I’m finishing my game in the testing phase. I want to publish it on gd.games. But I’m finding a difficulty related to sound. Currently the effect is weird: the entire game stops except for the music if I change tabs in browser or minimize it. This is a big problem for songs that need to be in sync with the game or that only play once.

In other topics I learned about the “PauseFocusLost” extension. But despite this name, “pause”, the music doesn’t actually pause. It simply turns down the volume. So, if someone click on another browser tab and return to the game a few minutes later, the unlooped song will probably have already ended, leaving the game without music.

Is there a way to pause the sounds and return them when the tab is reactivated? I tried:

But neither code worked (the music doesn’t play). These conditions are described to be used on Windows, Linux, and Mac. Maybe that’s why they don’t work for WEB.

edit.: I changed the category, as I suppose it’s a game engine bug.

1 Like

These don’t work on web as because they aren’t implemented and probably differ based on browser. If you know how to code in JS, you could attach a function on document blur that would stop the music and reenable on focus.

Though I think an alternative should exists. I’ll keep tinkering.

Thanks, Ansel!
I don’t know about JS Code… :disappointed:

Hey I created a unfocus/focus extension for you that works on the web.
Example Extension
Demo Project

2 Likes

Thanks for help-me, Ansel!!!

It seems to me that the situation is a little complicated.

Brave browser: it works if I switch to another window while the screen is focused on the game. If I change the tab doesn’t work.

Firefox: Works switching to another window or browser tab!

Both don’t work if I minimize the browser. And if I still have the game window open but click, for example, on the bar to write a new link, the song stops, but the rest of the game continues. Maybe I can set the TimeScale to 0…

My test:

Events:

1 Like