x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to Play a SoundCue in C++

Hello guys. I want to play an Audio in C++.

I have this SoundCue: alt text

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!

Product Version: UE 4.9
Tags:
1.png (194.6 kB)
more ▼

asked Nov 28 '15 at 08:09 PM in C++ Programming

avatar image

gedamial
127 38 37 50

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Oct 09 '16 at 10:57 PM

avatar image

bendemott
36 3 5

avatar image J0K3R_12QQ Feb 17 '17 at 06:03 PM

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?

(comments are locked)
10|2000 characters needed characters left

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 GetWorld() function. It's static function so dont need object (thats why it needs world instance), there also location based version of this function:

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Kismet/UGameplayStatics/PlaySoundAtLocation/2/index.html

Other then that there UAudioComponent which attach sound to actor

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UAudioComponent/index.html

as well as AAmbientSound which is simplest sound actor

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Sound/AAmbientSound/index.html

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 ;]

more ▼

answered Nov 29 '15 at 04:09 AM

avatar image

Shadowriver
35.2k 921 166 1098

avatar image gedamial Nov 29 '15 at 11:13 AM

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

avatar image Shadowriver Nov 30 '15 at 12:33 AM

I think they are optimal as they got defaults, you dont need to add them, also they are self explanatory, just think a little ;]

avatar image gedamial Nov 29 '15 at 11:16 AM

Also, how do I attach the USoundCue object to the sound I made with the blueprint below?

avatar image Shadowriver Nov 30 '15 at 12:34 AM

You suppose to post blueprint bellow?

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question