While using the UAudioComponent, I noticed what I can only assume is a bug
After spawning a sound with UGameplayStatics::SpawnSound2D() (or similar functions) then capturing the UAudioComponent reference, the reference to that UAudioComponent WILL BE LOST after about a minute (90% of the time it happens almost exactly at the 60 second mark).
And once the reference is lost, there doesn’t seem to be a way to get it back either, so the sound will keep on playing until it reaches the end of the sound or is terminated out due to some concurrency settings.
I have prepared a fresh project demonstrating this bug (see below code for link), but the key part is in the Tick() function of the Music Player below, which constantly “swaps (fade in/out)” between two different USounds (think of it as switching between two audio tracks of the same song).
You’ll notice that after about a minute, the error messages will start to trigger.
void AMusicPlayer::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bIsBGMPlaying)
{
currentTimeBGM += DeltaTime;
if (currentTimeBGM <= currentBGMlength)
{
if (timeToNextSwap < 0)
{
if (spawnedBGM_1->IsValidLowLevel() && spawnedBGM_2->IsValidLowLevel())
{
if (playingBGM1)
{
spawnedBGM_2->FadeIn(swapDuration, 1, currentTimeBGM);
spawnedBGM_1->FadeOut(swapDuration, 0);
}
else
{
spawnedBGM_1->FadeIn(swapDuration, 1, currentTimeBGM);
spawnedBGM_2->FadeOut(swapDuration, 0);
}
playingBGM1 = !playingBGM1;
timeToNextSwap = swapInterval;
}
else
{
FString Msg1 = ".";
FString Msg2 = ".";
if (!spawnedBGM_1->IsValidLowLevel()) Msg1 = "X";
if (!spawnedBGM_2->IsValidLowLevel()) Msg2 = "X";
UE_LOG(LogTemp, Error, TEXT("%s | %s"), *Msg1, *Msg2);
}
}
else
{
timeToNextSwap -= DeltaTime;
}
}
else
{
UE_LOG(LogTemp, Error, TEXT("==== TIMEOUT ===="));
spawnedBGM_1->Stop();
spawnedBGM_2->Stop();
bIsBGMPlaying = false;
}
}
}
I’m working on a work-around to this issue for the moment, but since I do believe this is NOT intended behavior, I’m sharing it hoping that someone can take a look at it one day.
Cheers.