Hi! I’m pretty new to C++, but I’ve been working with audio for a while. One of the first things I wanted to do was to create a SoundManager, a singleton class that persists across levels and manages important audio info and parametres, as well as handles high level music in the game(MainMenu, Level Transitions etc).
So I did just that, created a SoundManager class, derived from UObject, and store it in my GameInstance.
On Game instance init() I call SoundManager’s own init, where I handle UAudioComponent creation, setting sound and playing. But here’s the catch, it works perfectly fine while playing in Standalone, but I have completely no luck playing it in PIE Viewport.
No matter what I did, GEngine, GI, World, UAudioComponent and ActiveAudioDevice are valid. Please help me.
Code snippets below:
MyGI
void UMyGameInstance::Init()
{
Super::Init();
InitializeSoundManager();
}
void UMyGameInstance::InitializeSoundManager()
{
// TODO Need to laod SoundManagerBPClass from setting and not from path
UClass* SoundManagerBPClass = LoadClass<USoundManager>(nullptr, TEXT("/Game/Audio/Blueprints/BP_SoundManager.BP_SoundManager_C"));
if (SoundManagerBPClass != nullptr)
{
SoundManagerInstance = NewObject<USoundManager>(this, SoundManagerBPClass);
if (SoundManagerInstance != nullptr)
{
SoundManagerInstance->Initialize();
}
}
}
My SoundManager
void USoundManager::Initialize()
{
UMyGameInstance* MyGI = Cast<UMyGameInstance>(GetWorld()->GetGameInstance());
MainSoundComponent = NewObject<UAudioComponent>(GetTransientPackage(), NAME_None, RF_Transient);
MainSoundComponent->RegisterComponent();
MainSoundComponent->Activate();
MainSoundComponent->bAutoDestroy = true;
MainSoundComponent->bIgnoreForFlushing = true;
MainSoundComponent->bAllowSpatialization = false;
MainSoundComponent->bCanPlayMultipleInstances = false;
OnInitialize();
}
UAudioComponent* USoundManager::PlayMainUESound(USoundBase* SoundToPlay, float FadeInTimeDuration)
{
if (IsValid(SoundToPlay))
{
MainSoundComponent->SetSound(SoundToPlay);
MainSoundComponent->FadeIn(FadeInTimeDuration);
return MainSoundComponent;
}
else
{
return nullptr;
}
}
In my blueprint class I just call PlayMainUESound function.
Wwise works just fine
Please help me! Thank you.