オーディオを再生する
基本的な再生
インポートされたサウンドウェーブを再生するには、通常のサウンドと同様の関数を使用します。例えば、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
関数を使用して停止できます。
- Blueprint
- C++
// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Stop the playback of the sound wave
ImportedSoundWave->StopPlayback();
外部手段(例:オーディオコンポーネントでStopを呼び出すなど)を使用して音波の再生を停止することを推奨します。この関数は外部手段が利用できない場合にのみ使用してください。また、この関数はMetaSoundsからの再生には対応していないことに注意してください。
イベント処理
再生完了の追跡
オーディオ再生の終了を追跡するには、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();
手動でのメモリ解放は、特定のメモリ管理要件がある場合やガベージコレクタを無効にしている場合を除き、推奨されません。