Hello guys.
I want to play an Audio in C++.
I have this SoundCue:
How to play it in C++ code?
The only thing I found online is to create a new SoundCue pointer:
SoundCue* PickedSound;
Thanks in advance!
Hello guys.
I want to play an Audio in C++.
I have this SoundCue:
How to play it in C++ code?
The only thing I found online is to create a new SoundCue pointer:
SoundCue* PickedSound;
Thanks in advance!
Most simple function is PlaySound2D (can’t find it in API refrence for some reason):
UGameplayStatics::PlaySound2D(UObject* WorldContextObject, class USoundBase* Sound, float VolumeMultiplier, float PitchMultiplier, float StartTime)
You place SoundCue in to USoundBase argument, in WorldContextObject you place UWorld instance which you can easily get from any actor with () function. It’s static function so dont need object (thats why it needs world instance), there also location based version of this function:
Other then that there UAudioComponent which attach sound to actor
as well as AAmbientSound which is simplest sound actor
but i think you probably know how to use it, you can set them up in editor anyway
You just need to remeber that USoundCue, as well as USoundWave (you dont’t need to make cue for single staticwave) is extended from USoundBase, so both will fit in USoundBase* varables and arguments
I’m also asume you know how to set that USoundCue* varable you made, if not say in comment ;]
Well I decided to use the function you provided. I need to know what do parameters float VolumeMultiplier, float PitchMultiplier, float StartTime do, cause I don’t know which values assign them
Also, how do I attach the USoundCue object to the sound I made with the blueprint below?
I think they are optimal as they got defaults, you dont need to add them, also they are self explanatory, just think a little ;]
You suppose to post blueprint bellow?
Sound Cues aren’t played directly from source code, they are wrapped in UAudioComponent
first.
Example
USoundCue* propellerAudioCue;
UAudioComponent* propellerAudioComponent;
// Load our Sound Cue for the propeller sound we created in the editor...
// note your path may be different depending
// on where you store the asset on disk.
static ConstructorHelpers::FObjectFinder<USoundCue> propellerCue(
TEXT("'/Game/airplane-engine.airplane-engine'")
);
// Store a reference to the Cue asset - we'll need it later.
propellerAudioCue = propellerCue.Object;
// Create an audio component, the audio component wraps the Cue,
// and allows us to ineract with
// it, and its parameters from code.
propellerAudioComponent = CreateDefaultSubobject<UAudioComponent>(
TEXT("PropellerAudioComp")
);
// I don't want the sound playing the moment it's created.
propellerAudioComponent->bAutoActivate = false;
// I want the sound to follow the pawn around, so I attach it to the Pawns root.
propellerAudioComponent->AttachParent = RootComponent;
// I want the sound to come from slighty in front of the pawn.
propellerAudioComponent->SetRelativeLocation(FVector(100.0f, 0.0f, 0.0f));
// Attach our sound cue to the SoundComponent (outside the constructor)
if (propellerAudioCue->IsValidLowLevelFast()) {
propellerAudioComponent->SetSound(propellerAudioCue);
}
// Finally play the sound (outside the constructor)
propellerAudioComponent->Play();
// Also... if you want to set a parameter of the sound cue during runtime:
propellerAudioComponent->SetFloatParameter(FName("pitch"), 2500.f);
For a longer explanation see my blog article: playing sounds from c++ using Sound Cues
Is it weird, that I comment an answer to a question, that wasn’t even posted by me and is almost 4 months old? Anyway, your solution doesn’t work for me.
So I have a DefaultProjectile class with the UAudioComponent and I set the SoundCue in my DefaultProjectile.cpp and did everything that you did and it just does not work.
Help?