Download

Repeating the audio file in each frame in C++

Hello,

I’m trying to import audio component to an actor. I have added an audio property in the header file using the following piece of code:

UPROPERTY(EditAnyWhere)
UAudioComponent* MyAudio;

Then in the .cpp file I have the following code:

MyAudio = CreateDefaultSubobject<UAudioComponent>(TEXT(“Audio”));
MyAudio->AttachTo(RootComponent);

Right now I have the audio playing but since it’s a short audio file, it is just played once and then it stops.
I was wondering whether there is a way to play the audio in each frame using the existed “tick” function, then the sound is always played.

Thank you :slight_smile:

Can you explain why you want it to play every single frame? As in 60 times a second?

Even if it was something like static noise you’d be far, far better off repeating it inside an audio clip until it’s a few seconds long and then playing that back instead.

Anyway, one of the main ways I figure out how to do things in code is by doing them in blueprints first. It’s the same framework but blueprint examples are easier to come by and easier to figure out. Then you implement the same thing in code using the same members and functions.

Just check the ‘Looping’ box in the Sound Wave and Sound Cue that you use, it handles looping playback for you…