Play audio
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.
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);
Additionally, in UE versions up to 4.27, if you want to start playback from a specific time greater than 0, you must 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.
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();
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();
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();
You can stop the sound wave playback by using the StopPlayback
function. Please note that 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, take in mind that this function does not work for playback from MetaSounds.
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Stop the playback of the sound wave
ImportedSoundWave->StopPlayback();
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);
}
};
You can manually clear the audio data using the ReleaseMemory
function. It is not recommended to do manually, but it can be useful if you have disabled the garbage collector.
- Blueprint
- C++
// Release memory of the sound wave
ImportedSoundWave->ReleaseMemory();