how to sync audio and animation on the first run

So we have several scenes where an animation begins after an event occurs. In other words, we have an imported .ma file and an AudioClip that both get started simultaneously when something happens, kind of like:

EventHappened() {

We notice that the animation an audio are often not in sync. However, once the animation is loaded, if we press a Restart button that essentially calls the same function, then the animation and audio ARE in sync. For instance:

RestartButtonPressed() {

But they are only in sync if it is the second (or later) time calling EventHappened().

So I’m guessing that something happens when running the animation for the first time that has a side-effect of causing the audio to lag a bit. Or am I missing something simple?


It’s not uncommon to see a brief lag the first time you play an audio clip (loading, possibly uncompressing). I am guessing, here, but I have a hard time seeing what else would cause your problem.

I haven’t tried this, but you might be able to sync them up by getting/setting AnimationState.time and AudioSource.timeSamples for the two components.

Depending on how audio sources track their timing information, internally, you might even just be able to flip the two calls – play the audio, then the animation. If the audio caches its start time before loading, it won’t make a difference; if it does so after loading, it might help.

This all assumes my initial read of the problem is correct, of course.

This happened with one of my projects on Android. Audio would be playing slightly before animation (only on the first run). Setting AnimationState.time and AudioSrouce.timeSamples to zero before playing didn’t help. I noticed this happening only with 3D sounds though. If you can do with a 2D sound, it solves the problem. If not, I guess playing an audio triggered by an animation event would do the trick.

If I had to guess… Your sample rate is off. Double check that you’re animating in 60fps in Maya.