How to Play an Ambient Sound in c++

Hello, I want to be able to (in code) create an Ambient Sound while passing in the location of the .wav file, flag it to loop-able, then Play that ambient sound, and stop it if I need to in code. No blueprints. I am using Unreal for Rendering/Sound only. I have my own gameplay engine hooking into Unreal.

I figured it would look something like this but I can’t understand the code references in the docs. All I want to do is (pseudo code):

AAmbientSound* newSound;

newSound->SetAsset( “C:\Project\Assets\SomeAmbientSound.wav” );

newSound->SetLoopable( true );

newSound->Play();

newSound->Stop();

Any ideas on how to achieve that?

Like I mentioned, I won’t be using blueprints for this. I want to know how in code I can initialize and play an ambient sound. =)

You don’t need any coding for that, just place sound objects around the level, it’s more optimal way to do that, so you can have different rooms and locations with different ambient sound, engine can apply stereo effects to the sound based of player location. if you want global sound played whatever you are… just add audio component to player and sound will follow the player :slight_smile:

Also you should use SoundCues they will let you loop the sound and other interesting things, you also don’t get assets from file system for multiplatfrom compatibility and fact that your game can be installed anywhere, you need to import sound which will contain it to uasset (And then make SoundCue with it), then in C++ you can load it like this in contructor:

static ConstructorHelpers::FObjectFinder Sound(TEXT(“SoundCue’/Something/Something.Something’”));

And SoundCue will be loaded to variable Sound :slight_smile: you get address by right clicking asset in editor and copy reference code.

You seem to lacking knowledge about engine, i recommend you to play around with editor first to learn basics. Also you won’t avoid use of editor which you seem to want to do.

Why? What kind of ambient sound you want to make? Sound objects is not blueprint, they are coded in C++ in endine code. Also i updated my anwser as i got new ideas after :stuck_out_tongue:

Just want to make a normal Ambient Sound Play.

I’m not using the editor because I am using Unreal for rendering/sound only. I am using my own engine for gameplay.

So you at least using UObject tree? and do you use at least AActor?

if not, then i guess you should study AudioComponent class:

https://github.com/EpicGames/UnrealEngine/blob/4.2/Engine/Source/Runtime/Engine/Private/AudioComponent.cpp

And at first look i using AudioDevice class to make sound:

But what you doing sounds kind of crazy, this engine might be to to big for this kind of hacks. You could at least use UObject envrament

I’m having the exact same problem. How did you solve this?

The easiest way to do this in C++ is using the UGameplayStatics class. You have a lot of useful static functions in there, here are some :

UGameplayStatics::PlaySoundAtLocation()
UGameplayStatics::PlaySoundAttached()

Here is the documentation for the exact signature of those functions:

Hope this helps :slight_smile:

Mick

You can use a UE sound cue asset to play a .wav file as shown here: Audio Files in Unreal Engine | Unreal Engine 5.2 Documentation

Then you can use an UAudioComponent* yourWave to handle your sound cue. yourWave can be attached to the UE Sound Cue asset using the default object finder in your calss constructor:

yourWave = PCIP.CreateDefaultSubobject(this, TEXT(“soundKnockKnock”));
static ConstructorHelpers::FObjectFinder loadedSoundCue1(TEXT(“SoundCue’/yourSoundCue Path’”));
if (loadedSoundCue1.Object != NULL)
yourWave ->SetSound(loadedSoundCue1.Object);

To play yourWave:

yourWave ->SetWorldLocation(soundSourceLocation);
yourWave ->Play();