Creating a metronome through the precise scheduling of sounds with dspTime
In cases where we need precise scheduling of when sounds play, we should use the AudioSource.PlayScheduled(...)
method. This method uses the AudioSettings.dspTime
value, which is highly accurate based on the playing of music data through the Unity audio system. Another advantage of the dspTime
value is that is it independent of the graphical rendering frame rate:

Note that the dspTime
value is automatically frozen when a game is paused or suspended – so music scheduled using this approach will pause and resume flawlessly along with the scene gameplay. In this recipe, we'll create a metronome by precisely scheduling when two different sounds will play. Note that this recipe is based on some examples from the Unity documentation for the AudioSource.PlayScheduled(...)
method: https://docs.unity3d.com/ScriptReference/AudioSource.PlayScheduled.html.
Getting ready
For this recipe, we have provided two metronome sound clips...