Add audio clip started and finished events to audio source

This commit is contained in:
Chandler Cox
2025-08-01 15:07:25 -05:00
parent f37b75df7b
commit 5c7712daad
2 changed files with 25 additions and 1 deletions

View File

@@ -168,7 +168,7 @@ void AudioSource::Play()
else
{
// Source was nt properly added to the Audio Backend
LOG(Warning, "Cannot play unitialized audio source.");
LOG(Warning, "Cannot play uninitialized audio source.");
}
}
@@ -395,6 +395,9 @@ void AudioSource::Update()
AudioBackend::Source::VelocityChanged(SourceID, _velocity);
}
if (Math::NearEqual(GetTime(), _startTime) && _isActuallyPlayingSth && _startingToPlay)
ClipStarted();
// Reset starting to play value once time is greater than zero
if (_startingToPlay && GetTime() > 0.0f)
{
@@ -416,6 +419,7 @@ void AudioSource::Update()
{
Stop();
}
ClipFinished();
}
}
@@ -486,6 +490,7 @@ void AudioSource::Update()
{
Stop();
}
ClipFinished();
}
ASSERT(_streamingFirstChunk < clip->Buffers.Count());
@@ -583,3 +588,11 @@ void AudioSource::BeginPlay(SceneBeginData* data)
SetTime(GetStartTime());
}
}
void AudioSource::EndPlay()
{
Actor::EndPlay();
ClipStarted.UnbindAll();
ClipFinished.UnbindAll();
}

View File

@@ -76,6 +76,16 @@ public:
API_FIELD(Attributes="EditorOrder(10), DefaultValue(null), EditorDisplay(\"Audio Source\")")
AssetReference<AudioClip> Clip;
/// <summary>
/// Event fired when the audio clip starts.
/// </summary>
API_EVENT() Delegate<> ClipStarted;
/// <summary>
/// Event fired when the audio clip finishes.
/// </summary>
API_EVENT() Delegate<> ClipFinished;
/// <summary>
/// Gets the velocity of the source. Determines pitch in relation to AudioListener's position. Only relevant for spatial (3D) sources.
/// </summary>
@@ -326,4 +336,5 @@ protected:
void OnDisable() override;
void OnTransformChanged() override;
void BeginPlay(SceneBeginData* data) override;
void EndPlay() override;
};