オーディオを再生
インポートしたサウンドウェーブを再生するには、通常のサウンドウェーブと同じ関数を使用します。例えば、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
関数を使用して音波の再生を停止することができます。ただし、音波の再生を停止する際は外部の手段(例:オーディオコンポーネントでの停止呼び出し)を使用することが推奨されており、この関数は外部の手段が利用できない場合にのみ使用することをお勧めします。
また、この関数は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
関数を使用してオーディオデータを手動でクリアすることができます。手動で行うことは推奨されていません。
- ブループリント
- C++
// Release memory of the sound wave
ImportedSoundWave->ReleaseMemory();