issue with delay when playing an audio file immediately after another one

I’ve tried these solutions when trying to play an outro sfx, then an outro loop immediately after. unscaled time is because i reduce the timescale during the outro.

outroStart.Play();

yield return new WaitForSecondsRealtime(outroStart.clip.length)

outroLoop.Play();
outroStart.Play();

yield return WaitForUnscaled(outroStart.clip.length)

outroLoop.Play();
public static IEnumerator WaitForUnscaled(float seconds) {
        for (var timer = 0f; timer < seconds; timer += Time.unscaledDeltaTime) {
            yield return null;
        }
    }

In the editor, it works perfectly. When on android, I can notice a slight delay between the two sounds.

The issue will be because you’re using wait for seconds which uses a float time value, and isn’t accurate enough for this. It also means that the scheduling is dependent on frame timing.

You’ll need to use playscheduled instead, which uses doubles and is independent of frame rate.

see my article on playscheduled for an example of how to do this: How to Queue Audio Clips in Unity (the Ultimate Guide to PlayScheduled) - John Leonard French

hope that helps.

2 Likes