Merge branch 'audio-events' of https://github.com/Tryibion/FlaxEngine into Tryibion-audio-events

This commit is contained in:
Wojtek Figat
2025-09-23 17:07:18 +02:00
3 changed files with 80 additions and 2 deletions

View File

@@ -13,6 +13,8 @@ namespace FlaxEditor.CustomEditors.Dedicated
public class AudioSourceEditor : ActorEditor public class AudioSourceEditor : ActorEditor
{ {
private Label _infoLabel; private Label _infoLabel;
private Slider _slider;
private AudioSource.States _slideStartState;
/// <inheritdoc /> /// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout) public override void Initialize(LayoutElementsContainer layout)
@@ -28,6 +30,13 @@ namespace FlaxEditor.CustomEditors.Dedicated
_infoLabel = playbackGroup.Label(string.Empty).Label; _infoLabel = playbackGroup.Label(string.Empty).Label;
_infoLabel.AutoHeight = true; _infoLabel.AutoHeight = true;
// Play back slider
var sliderElement = playbackGroup.CustomContainer<Slider>();
_slider = sliderElement.CustomControl;
_slider.ThumbSize = new Float2(_slider.ThumbSize.X * 0.5f, _slider.ThumbSize.Y);
_slider.SlidingStart += OnSlidingStart;
_slider.SlidingEnd += OnSlidingEnd;
var grid = playbackGroup.UniformGrid(); var grid = playbackGroup.UniformGrid();
var gridControl = grid.CustomControl; var gridControl = grid.CustomControl;
gridControl.ClipChildren = false; gridControl.ClipChildren = false;
@@ -40,6 +49,38 @@ namespace FlaxEditor.CustomEditors.Dedicated
} }
} }
private void OnSlidingEnd()
{
foreach (var value in Values)
{
if (value is AudioSource audioSource && audioSource.Clip)
{
switch (_slideStartState)
{
case AudioSource.States.Playing:
audioSource.Play();
break;
case AudioSource.States.Paused:
case AudioSource.States.Stopped:
audioSource.Pause();
break;
default: break;
}
}
}
}
private void OnSlidingStart()
{
foreach (var value in Values)
{
if (value is AudioSource audioSource && audioSource.Clip)
{
_slideStartState = audioSource.State;
}
}
}
/// <inheritdoc /> /// <inheritdoc />
public override void Refresh() public override void Refresh()
{ {
@@ -51,7 +92,29 @@ namespace FlaxEditor.CustomEditors.Dedicated
foreach (var value in Values) foreach (var value in Values)
{ {
if (value is AudioSource audioSource && audioSource.Clip) if (value is AudioSource audioSource && audioSource.Clip)
{
text += $"Time: {audioSource.Time:##0.0}s / {audioSource.Clip.Length:##0.0}s\n"; text += $"Time: {audioSource.Time:##0.0}s / {audioSource.Clip.Length:##0.0}s\n";
_slider.Maximum = audioSource.Clip.Length;
_slider.Minimum = 0;
if (_slider.IsSliding)
{
if (audioSource.State != AudioSource.States.Playing)
{
// Play to move slider correctly
audioSource.Play();
audioSource.Time = _slider.Value;
}
else
{
audioSource.Time = _slider.Value;
}
}
else
{
_slider.Value = audioSource.Time;
}
}
} }
_infoLabel.Text = text; _infoLabel.Text = text;
} }

View File

@@ -167,8 +167,8 @@ void AudioSource::Play()
} }
else else
{ {
// Source was nt properly added to the Audio Backend // Source was not properly added to the Audio Backend
LOG(Warning, "Cannot play unitialized audio source."); LOG(Warning, "Cannot play uninitialized audio source.");
} }
} }
@@ -401,6 +401,9 @@ void AudioSource::Update()
_startingToPlay = false; _startingToPlay = false;
} }
if (Math::NearEqual(GetTime(), _startTime) && _isActuallyPlayingSth && _startingToPlay)
ClipStarted();
if (!UseStreaming() && Math::NearEqual(GetTime(), 0.0f) && _isActuallyPlayingSth && !_startingToPlay) if (!UseStreaming() && Math::NearEqual(GetTime(), 0.0f) && _isActuallyPlayingSth && !_startingToPlay)
{ {
int32 queuedBuffers; int32 queuedBuffers;
@@ -416,6 +419,7 @@ void AudioSource::Update()
{ {
Stop(); Stop();
} }
ClipFinished();
} }
} }
@@ -486,6 +490,7 @@ void AudioSource::Update()
{ {
Stop(); Stop();
} }
ClipFinished();
} }
ASSERT(_streamingFirstChunk < clip->Buffers.Count()); ASSERT(_streamingFirstChunk < clip->Buffers.Count());

View File

@@ -76,6 +76,16 @@ public:
API_FIELD(Attributes="EditorOrder(10), DefaultValue(null), EditorDisplay(\"Audio Source\")") API_FIELD(Attributes="EditorOrder(10), DefaultValue(null), EditorDisplay(\"Audio Source\")")
AssetReference<AudioClip> Clip; AssetReference<AudioClip> Clip;
/// <summary>
/// Event fired when the audio clip starts.
/// </summary>
API_EVENT() Action ClipStarted;
/// <summary>
/// Event fired when the audio clip finishes.
/// </summary>
API_EVENT() Action ClipFinished;
/// <summary> /// <summary>
/// Gets the velocity of the source. Determines pitch in relation to AudioListener's position. Only relevant for spatial (3D) sources. /// Gets the velocity of the source. Determines pitch in relation to AudioListener's position. Only relevant for spatial (3D) sources.
/// </summary> /// </summary>