Play audio
Basic Playback
To play an imported sound wave, use the same functions as you would for a regular one. For instance, use the PlaySound2D
or Play
function from an audio component such as Sound Cue.
Controlling Playback
Rewinding Playback Time
To rewind the playback time of the sound wave, use the RewindPlaybackTime
function.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Rewind playback time of the sound wave for 12.5 seconds
ImportedSoundWave->RewindPlaybackTime(12.5f);
In UE versions up to 4.27, if you want to start playback from a specific time greater than 0, you may need to use the RewindPlaybackTime
function beforehand. Otherwise, the sound may not play back correctly due to internal engine issues handling procedural waves. This issue has been resolved in the engine since version 5.0.
Getting Playback Information
To get the current playing time of the sound wave, use the GetPlaybackTime
or GetPlaybackPercentage
functions. You can also obtain the duration of the sound wave by using the GetDuration
function.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Get the current playback time of the sound wave
float PlaybackTime = ImportedSoundWave->GetPlaybackTime();
// Get the current playback percentage of the sound wave
float PlaybackPercentage = ImportedSoundWave->GetPlaybackPercentage();
// Get the duration of the sound wave
float Duration = ImportedSoundWave->GetDuration();
Checking Playback Status
Is Currently Playing
To determine whether the sound wave is currently playing, you can use the IsPlaying
function.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Check if the sound wave is currently playing
bool bIsPlaying = ImportedSoundWave->IsPlaying();
Is Playback Finished
To check if the sound wave has finished playing, you can use the IsPlaybackFinished
function.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Check if the sound wave has finished playback
bool bIsFinished = ImportedSoundWave->IsPlaybackFinished();
Stopping Playback
You can stop the sound wave playback by using the StopPlayback
function.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Stop the playback of the sound wave
ImportedSoundWave->StopPlayback();
It is recommended to stop the sound wave playback using external means (e.g., by calling Stop on the audio component) and to use this function only if external means are not available. Also, note that this function does not work for playback from MetaSounds.
Event Handling
Tracking Playback Completion
To track the end of audio playback, bind to the OnAudioPlaybackFinished
delegate.
- Blueprint
- C++
UCLASS()
class AMyAudioPlayer : public AActor
{
GENERATED_BODY()
public:
UFUNCTION()
void OnAudioFinished()
{
// Handle the end of audio playback
}
void BindAudioDelegate(UImportedSoundWave* ImportedSoundWave)
{
// Bind to the OnAudioPlaybackFinished delegate
ImportedSoundWave->OnAudioPlaybackFinished.AddDynamic(this, &AMyAudioPlayer::OnAudioFinished);
}
};
Memory Management
Releasing Memory
You can manually clear the audio data using the ReleaseMemory
function.
- Blueprint
- C++
// Release memory of the sound wave
ImportedSoundWave->ReleaseMemory();
Manual memory release is not recommended unless you have specific memory management requirements or have disabled the garbage collector.