Audio Component Play not working

Sorry for a bit clickbait title. The problem for me is in fact Play() not playing sound attached to UAudioComponent. However I’d guess I just do something wrong, but can’t figure out what several days already. Does anyone have an idea?

Here is how the object is declared in my game instance class header:

		UAudioComponent* idleVillage;

Then later it is created in the constructor:

	idleVillage = CreateDefaultSubobject<UAudioComponent>(TEXT("IdleVillage"));

In Blueprint class deriving from my game instance class I then added the sound

And then finally I call play somewhere in my code

	UE_LOG(LogTemp, Log, TEXT("ChangeMusicState call received"));
	currentMusicState = newState;


By logs I can see that the method is called, but the music is not playing. I tried playing it via UGameplayStatics, like this

	UGameplayStatics::PlaySound2D(this, idleVillage->Sound);

And then it does play, but that’s rather bad of a workaround since I need to be able to stop the sound.
So once again, does anyone have any idea what am I doing wrong? (If any additional informations are required, just ask please, I will proivide them)

In your constructor:

idleVillage = CreateDefaultSubobject<UAudioComponent>(TEXT("IdleVillage"));


UE_LOG(LogTemp, Log, TEXT("ChangeMusicState call received"));
currentMusicState = newState;

// idleVillage->Activate(); Not sure if you need this
1 Like

The sound you are trying to play could be optimized away, this happens when the volume is 0 or when you are far away I believe. On the sound asset editor configure it to play virtualized when silent.

*Edit You can also do that in c++.

// USoundBase* Sound
Sound->VirtualizationMode = EVirtualizationMode::PlayWhenSilent;
1 Like

Thanks for reply!

This sound is already set in deriving class as visible in screen in OP, but I will upload that again:

Also I don’t think Game Instance types have Root Component since they are not scene components, hmm

Thanks for the reply!
I tried to set this both in Blueprints and CPP, but unfortuantelly no effect :frowning:

Good point, unsure about this. I made a music player once as a GameInstanceSubsystem but I just realized I use CreateSound2D to play the audio.

UObject* WorldContext = GetOuter()->GetWorld();
Sound->VirtualizationMode = EVirtualizationMode::PlayWhenSilent;

AudioComponent = UGameplayStatics::CreateSound2D(WorldContext, Sound, 1.0f, 1.0f, 0.0f, nullptr, true, false);

I don’t use CreateDefaultSubobject or SetupAttachment there, I simply have an array of structs and each struct holds an UAudioComponent pointer among some other data.


Ok I found out what is wrong. It seems components added to GameInstance are kind of ignored, since they require World Context to exist. I tried attaching AudioComponent to a sub-Character class and call Play and everything works. It seems that Game Instance does not support this, sadly.

Once again, thanks for answers!

I’m using AudioComponent in a GameInstance for persistent music, make sure you are calling UGameplayStatics::CreateSound2D when a level is loaded ( BeginPlay for example ), not in the constructor.
I’m using UGameplayStatics::CreateSound2D in a function called by BeginPlay when the first level is loaded.

Game Instance does not have BeginPlay, nor in CPP, nor in Blueprints, nor in docs. I’m sorry, but are you not confusing it with Game Mode?

I’m calling GameInstance function in a GameMode’s BeginPlay ( could be in any Actor ). GameMode is per level, GameInstance per game.

1 Like