ActiveSound->PlaybackTime out of sync after game loses focus


I’m currently syncing the levels to the music by checking the current playback time of the music track:

FAudioDevice* AudioDevice = GEngine->GetAudioDevice();
FActiveSound* ActiveSound = AudioDevice->FindActiveSound(AudioComponent);
return ActiveSound->PlaybackTime;

This works fine, but if the game runs in true fullscreen, and I e.g. press WIN+D zu minimize it and then maximize it again, the PlaybackTime variable is out of sync with the music, i.e. it gives a wrong time. I guess because of the short lag/stuttering while the window is minimizing, the music or game time lags more than the other, causing this issue?

In windowed fullscreen this is not an issue, as the game continues to run without a break while minimizing.

Any ideas on how I could prevent this in true fullscreen? Maybe pausing the game before minimizing or something.

So, I just tried to handle the window focus events to manually pause/unpause when focus changes. Works fine, but doesn’t help me, as the focus lost event seems to be sent too late, so the async issue happens before the game pauses.

Next thing I’m going to try is to restart the music track (when unpausing) at the playback time the game thinks the track currently is at, so they get resynced. (**** those long sentences)

Any other ideas or help? Thanks!

Okay, that last workaround idea actually works. In the focus received event, I’m restarting the music track at the given playback position (which is bugged after maximizing the game window), so both are in sync again.

I have to recalculate the actual start time though (modulo the track length), as the playback position just continues counting even if the a loop of the track has ended and it restarts at zero, e.g. the playback position reads 65sec if a 60sec long track has looped and is at second 5. Using the 65sec as the start position of the music just starts the track from the beginning, so I have to use modulo to get the “5 sec”.

I don’t like ActiveSound->PlaybackTime.