播放音訊
基本播放
要播放已匯入的音波,請使用與一般音波相同的函式。例如,使用 PlaySound2D 或來自音訊元件(如 Sound Cue)的 Play 函式。

控制播放
倒轉播放時間
若要倒轉音波的播放時間,請使用 RewindPlaybackTime 函式。
- 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);
備註
在 UE 4.27 及更早版本中,如果您想从大于 0 的特定时间点开始播放,可能需要事先使用 RewindPlaybackTime 函数。否则,由于引擎内部处理程序性波形时存在的问题,声音可能无法正常播放。此问题自引擎 5.0 版本起已得到解决。
获取播放信息
要获取声音波形的当前播放时间,请使用 GetPlaybackTime 或 GetPlaybackPercentage 函数。您还可以通过 GetDuration 函数获取声音波形的持续时间。
- 蓝图
- 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();
檢查播放狀態
目前正在播放
若要判斷音波是否正在播放,您可以使用 IsPlaying 函式。
- Blueprint
- C++

// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Check if the sound wave is currently playing
bool bIsPlaying = ImportedSoundWave->IsPlaying();
播放是否完成
若要檢查音波是否已播放完畢,您可以使用 IsPlaybackFinished 函式。
- Blueprint
- C++

// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Check if the sound wave has finished playback
bool bIsFinished = ImportedSoundWave->IsPlaybackFinished();
停止播放
您可以透過使用 StopPlayback 函數來停止音波播放。
- Blueprint
- C++

// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Stop the playback of the sound wave
ImportedSoundWave->StopPlayback();
資訊
通常建議使用外部方式停止音波播放(例如,在音訊元件上呼叫 Stop),並在無法使用外部方式時使用此函數。此外,請注意此函數不適用於 MetaSounds 的播放。
事件處理
追蹤播放完成
若要追蹤音訊播放的結束,請繫結到 OnAudioPlaybackFinished 委派。
- 藍圖
- 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);
}
};
記憶體管理
釋放記憶體
您可以使用 ReleaseMemory 函數手動清除音訊資料。
- Blueprint
- C++

// Release memory of the sound wave
ImportedSoundWave->ReleaseMemory();
注意
不建議手動釋放記憶體,除非您有特定的記憶體管理需求,或已停用垃圾回收器。