How can I keep Audio from losing focus when not in Editor?

Hello. I have an Ambient looping Audio playing in my game, but it only plays when I click the game window. When I have my mouse click somewhere off screen, it stops playing. I know this is probably an issue with OnFocus. Is there a way in Blueprint to play the Audio continuously, even when the game is NOT in focus?

I know Unity has an Application.OnFocus function that sends an event for the game being onfocus or not. I am looking for a way to do something similar- hopefully just setting music to keep playing EVEN when the game is not in focus.

If anyone is still wondering, I figured it out!

The engine automatically mutes itself whenever the window is not focused.

In WindowsPlatformMisc.cpp:

// if its our window, allow sound, otherwise silence it
GVolumeMultiplier = HasFocus ? 1.0f : 0.0f;

All I did was comment out that line and viola- it works!

Hi
So it’s needed a full recompilation of the Engine or it’s enough to override that function somewhere in my project?

Yes, after you comment out that line, you will need to recompile your code in order for it to work. I was never able to find a way in Blueprint to do it.

So, comment out that line of code, recompile, then you should be good to go.

This has been moved to the Engine.ini file as of 4.6:

GConfig->GetFloat(TEXT("Audio"), TEXT("UnfocusedVolumeMultiplier"), UnfocusedVolumeMultiplier, GEngineIni);

In the DefaultEngine.ini or any of the Engine config files where you would need to change this just set:

[Audio]
UnfocusedVolumeMultiplier=1.0
6 Likes

Just out of curiosity, why would you want to do this? Personally I find it irritating when I minimize a game and its audio keeps playing.

If the Engine is open you’ll need to restart ir for the changes take effect.

Hi, do you know if this works for mobile devices too? (iOS & Android)

No, I don’t know, please post your findings for others. Thanks!

preferably you want to enable/disable this feature in game menu.

why would you want to do this? imagine you have 2 monitors, playing game where sound is important, and then you need to alt+tab to the second monitor (you still can see whats happening on the other monitor, if you play windowed fullscreen mode, but you will not hear sounds you want to hear even alt-tabbed for a while to a chat or something you have on the other screen)

Just press this box

4 Likes

This is an editor setting, not a setting for the shipped build.

1 Like

You are a legend! Thank you!

Thanks!
I’m working on UE4.27.
The file DefaultEngine.ini is under <PROJECT_NAME>/Config/DefaultEngine.ini.
Add that setting in the bottom and it works!

1 Like

Now the real question is how do we turn this into a toggle switch in the game settings menu in the shipped product so people can turn it on and off like I see in hundreds of other games.

1 Like

FApp::GetUnfocusedVolumeMultiplier() and FApp::SetUnfocusedVolumeMultiplier(float InVolumeMultiplier). It updates GEngineIni. You can use your own UPROPERTY(config) to drive it too I suppose.

1 Like