Added "Start Time" option for Audio Source Actor

Added ability to set the start time of playback if "Play On Start" is enabled.
This commit is contained in:
whocares77
2024-01-24 19:58:29 +03:00
parent 1094abce5a
commit 39e7be6322
2 changed files with 25 additions and 1 deletions

View File

@@ -19,6 +19,7 @@ AudioSource::AudioSource(const SpawnParams& params)
, _minDistance(1000.0f)
, _loop(false)
, _playOnStart(false)
, _startTime(0.0f)
, _allowSpatialization(true)
{
Clip.Changed.Bind<AudioSource, &AudioSource::OnClipChanged>(this);
@@ -71,6 +72,11 @@ void AudioSource::SetPlayOnStart(bool value)
_playOnStart = value;
}
void AudioSource::SetStartTime(float value)
{
_startTime = value;
}
void AudioSource::SetMinDistance(float value)
{
value = Math::Max(0.0f, value);
@@ -361,6 +367,7 @@ void AudioSource::Serialize(SerializeStream& stream, const void* otherObj)
SERIALIZE_MEMBER(DopplerFactor, _dopplerFactor);
SERIALIZE_MEMBER(Loop, _loop);
SERIALIZE_MEMBER(PlayOnStart, _playOnStart);
SERIALIZE_MEMBER(StartTime, _startTime);
SERIALIZE_MEMBER(AllowSpatialization, _allowSpatialization);
}
@@ -377,6 +384,7 @@ void AudioSource::Deserialize(DeserializeStream& stream, ISerializeModifier* mod
DESERIALIZE_MEMBER(DopplerFactor, _dopplerFactor);
DESERIALIZE_MEMBER(Loop, _loop);
DESERIALIZE_MEMBER(PlayOnStart, _playOnStart);
DESERIALIZE_MEMBER(StartTime, _startTime);
DESERIALIZE_MEMBER(AllowSpatialization, _allowSpatialization);
DESERIALIZE(Clip);
}
@@ -540,5 +548,6 @@ void AudioSource::BeginPlay(SceneBeginData* data)
return;
#endif
Play();
SetTime(GetStartTime());
}
}

View File

@@ -52,6 +52,7 @@ private:
float _dopplerFactor = 1.0f;
bool _loop;
bool _playOnStart;
float _startTime;
bool _allowSpatialization;
bool _clipChanged = false;
@@ -148,11 +149,25 @@ public:
return _playOnStart;
}
/// <summary>
/// Determines the time (in seconds) at which the audio clip starts playing if Play On Start is enabled.
/// </summary>
API_PROPERTY(Attributes = "EditorOrder(51), DefaultValue(0.0f), Limit(0, float.MaxValue, 0.01f), EditorDisplay(\"Audio Source\", \"Start Time\"), VisibleIf(nameof(PlayOnStart))")
FORCE_INLINE float GetStartTime() const
{
return _startTime;
}
/// <summary>
/// Determines whether the audio clip should auto play on game start.
/// </summary>
API_PROPERTY() void SetPlayOnStart(bool value);
/// <summary>
/// Determines the time (in seconds) at which the audio clip starts playing if Play On Start is enabled.
/// </summary>
API_PROPERTY() void SetStartTime(float value);
/// <summary>
/// Gets the minimum distance at which audio attenuation starts. When the listener is closer to the source than this value, audio is heard at full volume. Once farther away the audio starts attenuating.
/// </summary>