播放音频
要播放导入的声音波形,使用与常规声音相同的函数。例如,可以使用音频组件(如 Sound Cue)的 PlaySound2D
或 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
函数停止声波回放。请注意,建议使用外部方式停止声波回放(例如,通过调用音频组件上的 Stop),并仅在无法使用外部手段时使用此函数。
另外需要注意的是,此函数不适用于从 MetaSounds 的回放。
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Stop the playback of the sound wave
ImportedSoundWave->StopPlayback();
要跟踪音频播放结束,请绑定到 OnAudioPlaybackFinished
委托。
- 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);
}
};
你可以使用 ReleaseMemory
函数手动清除音频数据。虽然不建议手动执行,但在禁用垃圾收集器时可能会很有用。
- Blueprint
- C++
// Release memory of the sound wave
ImportedSoundWave->ReleaseMemory();