No Audio after resuming PC(Windows) from Sleep

The issue is that when the computer is put to sleep while the game is running, on resume audio will no longer render. I believe this is happening because the audio device has likely been recreated by the OS. But there doesn’t appear to be an event notifying of a device change. (There are other device change events fired, but only for the Communicates role, not for the Console role.)

Is there a common solution for this?

I’ve thought to manually reset the audio device on resume, although I have yet to find an engine event that appears to trigger for this on Windows. FCoreDelegates::ApplicationHasEnteredForegroundDelegate or FCoreDelegates::ApplicationHasReactivatedDelegate don’t appear to be broadcasted on Windows.

Does the engine have an event that fires on app resume from sleep/suspend on Windows?

[Attachment Removed]

Steps to Reproduce
From a new project, create a level that plays a sound in a loop.

Run game as standalone from editor or from a built package.

Send computer to sleep.

Wake computer after it goes to sleep.

No audio will render in game when it resumes.

Relaunch game or swap output devices back and forth to get audio working again.

[Attachment Removed]

Hi Jake,

Sorry for taking a little time to get back to you on this while I investigated it. I have confirmed that we do not get a callback from sleep/wake mode in Windows and we have started an internal code dive on this to look at an engine level fix.

The game level fix would be to tap into the RegisterSuspendResumeNotification and call RequestDeviceSwap() when you receive a notification. If you want to call the device swap from C++, you would need something like:

if (FAudioDeviceManager* DeviceManager = FAudioDeviceManager::Get())
{
    if (FAudioDevice* AudioDevice = DeviceManager->GetMainAudioDeviceRaw())
    {
        if (Audio::IAudioMixerPlatformInterface* MixerPlatform = static_cast<Audio::FMixerDevice*>(AudioDevice)->GetAudioMixerPlatform())
        {
            MixerPlatform->RequestDeviceSwap(TEXT(""), /*bInForce*/ true, TEXT("MyGame::OnSystemResumedFromSleep"));
        }
    }
}

The idea of recognizing the sleep/wake cycle and calling a device swap should work on both XAudio2 or Wasapi backends.

[Attachment Removed]

It sounds like there are two problems here - when to call for a device swap, and ensuring you are calling it for the right device.

Unfortunately, since this we do not get a callback from the OS on this, I don’t have any solid guidance on when it is good to request a device swap. The non-determinism of a timer is unfortunate, but it may be the best option for now.

As for the actual device, you can cache the last successful device ID and re-request that when you wake up. Otherwise, requesting TEXT(“”) will give you the default device which is the headphone port on the motherboard in this case.

[Attachment Removed]

Hi Richard, thanks for the reply.

I’ve implemented the notification you recommending and that does appear to be firing on suspend and resume. But I am having a bit of trouble with the RequestDeviceSwap. Calling it immediately on resume has not worked.

Using the delegate UAudioDeviceNotificationSubsystem::DeviceSwitchedNative, there is an initial broadcast with the correct DeviceID, but then soon after another broadcast with the wrong DeviceID. (In my case the default output device is the motherboard audio out, but after resume the output gets switched the monitor’s audio out device.)

From the log I think it’s failing to initialize to the default device and creating a null device. And then switches to the next available device. This appears to happen with or without my changes to request the device swap.

[Attachment Removed]

Attaching a snippet of the log going through a suspend/resume cycle.

[Attachment Removed]

I’ve found that requesting the device swap too earlier will cause it fail. It seems to need to happen after the swap to the other output. Adding an arbitrary delay does work but I’d like to find a way that is more deterministic. But I haven’t yet found a public method to detect that the primary device failed to initialize.

[Attachment Removed]

1 second didn’t always work, and 2 seemed okay. But it appears to go through every possible output device on the machine which could be more or less depending on the setup.

The solution I ended up with was to start an async background task that will sleep in loop. With callbacks from AudioDeviceNotificationSubsystem::DefaultRenderDeviceChangedNative adding to the sleep count. This lets it scale with the number of output devices. When the count reaches zero then call RequestDeviceSwap(TEXT(“”)…) to get the system default device.

So far this seems to be working, with only a slight delay before audio comes back. But it’s not too noticeable with everything else going on during a resume from sleep.

[Attachment Removed]