refactor: use PlayDelayed API for audio delay and add Stop by ID support

- Replace PlayAfterDelay coroutine with AudioSource.PlayDelayed
- Add Stop(audioId) public API
- Add startWithMusic toggle
- Clean up namespace and debug macros
This commit is contained in:
2026-04-17 11:20:17 +08:00
parent 2b34d0bf94
commit accd311e1c
10 changed files with 291 additions and 36 deletions
@@ -12,9 +12,9 @@ namespace OCES.Audio
internal class SyncPointState
{
public SyncPoint Mode;
public int BaseTimeSamples; // 读取Source 的 timeSamples
public double BaseDspTime; // 读取Source 的 dspTime
public int SampleRate; // Source 的采样率
public int BaseTimeSamples; // 读取Source Segment 的 timeSamples
public double BaseDspTime; // 读取Source Segment 的 dspTime
public int SampleRate; // Source Segment 的采样率
public bool Ready;
}
@@ -107,9 +107,9 @@ namespace OCES.Audio
if (syncState is { Mode: SyncPoint.SameAsCurrentSegment })
{
AudioSource newSource = CurrentHandle?.GetFirstLeafSource();
if (newSource != null && newSource.clip != null)
if (newSource && newSource.clip)
{
// 计算从读取Source 到现在经过的 samples
// 计算从读取Source Segment 到现在经过的 samples
double elapsedSeconds = AudioSettings.dspTime - syncState.BaseDspTime;
int elapsedSamples = (int)(elapsedSeconds * syncState.SampleRate);
int targetTimeSamples = syncState.BaseTimeSamples + elapsedSamples;