I am having an issue with the UAudioComponent
SetSound()
function. It plays the sound as soon as the sound is set, even though I have set AutoActivate to false.
The only forum posts I could find on this issue are here and here. Both reference setting AutoActivate to false, which I have done, but the audio still plays when SetSound is executed.
I am using UE 4.26.2 and C++. The relevant code is posted below:
Header:
UPROPERTY(EditAnywhere, Category = Sound, meta = (AllowPrivateAccess = "true"))
class UAudioComponent* RollingSound;
UPROPERTY(EditAnywhere, Category = Sound, meta = (AllowPrivateAccess = "true"))
class USoundBase* RollingSoundWave;
Constructor:
RollingSound = CreateDefaultSubobject<UAudioComponent>(TEXT("RollingSound"));
static ConstructorHelpers::FObjectFinder<USoundBase> RollingSoundHelper(TEXT("SoundCue'/Game/Sound/BallRollingCue.BallRollingCue'"));
if (RollingSoundHelper.Object != nullptr)
{
RollingSound->SetAutoActivate(false);
RollingSoundWave = RollingSoundHelper.Object;
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Red, TEXT("Failed to load ball rolling sound cue"));
}
I am currently executing SetSound in PostInitializeComponents() (per advice in one of the above forum posts) but it does not matter where I execute it, I get the same result. I have tried in the constructor, BeginPlay and the function where the sound is played and I get the same results.
if (RollingSoundWave != nullptr)
{
//RollingSound->UAudioComponent::SetSound(RollingSoundWave);
RollingSound->SetSound(RollingSoundWave);
}
When I play in editor and look at the properties of the pawn I can see that AutoActivate is unchecked but the sound is still playing once when the game loads and again when the level loads. I have tried executing Deactivate() and Stop() before SetSound() but this has no effect.
Any help would be appreciated.
[UE4-26] [Audio] [CPP]