Preventing AudioComponent from being destroyed

Avengar
Tue, 29 May 2018 14:49:29 +0000
Audio

Hello there!

Going straight to the problem. I’m trying to create UAudioComponent that can be paused and hold into memory until I decide to delete it. Now i have problem with it becouse after some time on level my sound just disappears.

This is how I’m creating it right now:

UAudioComponent* UAudioController::Sound2D(UObject* WorldContextObject, class USoundBase* Sound, float VolumeMultiplier, float PitchMultiplier, float StartTime)
{
	if (!Sound)
	{
		return nullptr;
	}

	UAudioComponent* AudioComponent = UGameplayStatics::CreateSound2D(WorldContextObject, Sound, VolumeMultiplier, PitchMultiplier, StartTime);
	PlaybackPositionMap.Add(AudioComponent, 0.f);
	BindDelegates(AudioComponent);
	AudioComponent->Play(StartTime);
	return AudioComponent;
}

PlaybackPositionMap is just TMap that holds reference to UAudioComponent and float that is time played from that sound.

BindDelegates function looks like this:

void UAudioController::BindDelegates(UAudioComponent* AudioComponent)
{
	if (AudioComponent->IsValidLowLevel())
	{
		AudioComponent->OnAudioPlaybackPercent.AddDynamic(this, &UAudioController::SavePlaybackPosition);
		AudioComponent->OnAudioFinished.AddDynamic(this, &UAudioController::SoundFinished);
	}
}

I do not think you need it but just to be clear with it. I tried saving reference to UAudioComponent in arrays maps static components and disallowing it’s destruction but it always dissapears. Any way I can change that?

2 posts - 1 participant