播放音频
基础播放
要播放导入的声波,使用与常规声波相同的函数。例如,使用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
函数获取音波的总时长。
- 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();
检查播放状态
是否正在播放
要确定声波当前是否正在播放,可以使用 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();
注意
除非您有特定的内存管理需求或已禁用垃圾回收器,否则不建议手动释放内存。