オーディオを再生
基本的な再生
インポートしたサウンドウェーブを再生するには、通常のサウンドウェーブと同じ関数を使用します。例えば、Sound Cue などのオーディオコンポーネントから PlaySound2D または Play 関数を使用します。

再生の制御
再生時間の巻き戻し
サウンドウェーブの再生時間を巻き戻すには、RewindPlaybackTime 関数を使用します。
- ブループリント
- 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 関数を使用できます。
- ブループリント
- C++

// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Check if the sound wave is currently playing
bool bIsPlaying = ImportedSoundWave->IsPlaying();
再生が完了したか
サウンドウェーブの再生が完了したかどうかを確認するには、IsPlaybackFinished 関数を使用できます。
- ブループリント
- C++

// Assuming ImportedSoundWave is a UE reference to a UImportedSoundWave object
// Check if the sound wave has finished playback
bool bIsFinished = ImportedSoundWave->IsPlaybackFinished();
再生を停止する
StopPlayback 関数を使用することで、音声波形の再生を停止できます。
- ブループリント
- 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 関数を使用して、オーディオデータを手動でクリアできます。
- ブループリント
- C++

// Release memory of the sound wave
ImportedSoundWave->ReleaseMemory();
ガベージコレクタを無効化している場合や、特定のメモリ管理要件がある場合を除き、手動でのメモリ解放は推奨されません。
再生されたオーディオデータの解放
再生中にオーディオバッファの再生済み部分を自動的に解放するには、SetPlayedAudioDataReleaseSettings 関数を使用して解放設定を構成します。
- ブループリント
- C++

FPlayedAudioDataReleaseSettings ReleaseSettings;
ReleaseSettings.bReleasePlayedAudioData = true;
ReleaseSettings.ReleaseCallbackFrequency = 50;
ReleaseSettings.MinFramesToRelease = 44100;
// Configure automatic release of the already-played audio data
ImportedSoundWave->SetPlayedAudioDataReleaseSettings(ReleaseSettings);
bReleasePlayedAudioData- 解放が有効かどうか。デフォルトでは無効ReleaseCallbackFrequency- 解放チェック間の音声生成リクエスト(OnGeneratePCMAudioの呼び出し)の数MinFramesToRelease- 解放をトリガーするために必要な再生済みフレームの最小数
これを有効にすると、すでに解放されたポイントより前への巻き戻し/シークが制限されます。その音声データは物理的に解放されており、再生できなくなるためです。