Audio Sync Advice Needed

Hiā€¦ so, Iā€™m not sure that the following is particularly Needle and/or Unity-related but Iā€™m hoping someone might be able to help.

Basically, I have a .glb which has a looping animation set to ā€˜Play Automaticallyā€™ using the animation component. This works perfectly and the animation plays exactly as expected.

I also have an audio track which needs to accompany the animation and run/loop in sync with it. However, of course even with ā€˜Play On Awakeā€™ checked in the ā€˜Audio Sourceā€™ component the track wonā€™t begin playing until there has been some user interaction (i.e. rotating the view of the scene or similar.)ā€¦ by which time itā€™s out of sync with the animation which has already been running.

I know that the ā€˜user interaction to start audioā€™ thing has always existedā€¦ with all web contentā€¦ since the dawn of time :upside_down_face: ā€¦ but Iā€™m trying to ascertain what might be the best solution in this instance? I donā€™t necessarily need the animation to start right away, so perhaps, instead of it playing automatically, thereā€™s a way to set the animation to only begin playing on user interaction, in the same way as the audio so they run in sync with each other?

Or maybe thereā€™s a better way to achieve what Iā€™m aiming for?

Any thoughts or advice will be very welcome :slightly_smiling_face:

Original Post on Discord

by user 908977119781060648

:wave: so usually this is solved by having some sort of ā€œPlayā€ button front and center, and when someone clicks that both audio and animation play in sync

For actually playing in sync and not deviating: usually you check the time offset between audio and animation and decide which one should be corrected if the time offset becomes bigger than, say, 0.1s

We do this internally for Timeline tracks - so if you just add your animation and audio to a Timeline in Unity that will be taken care of already

As felix said: all you need to do is playing your animation via a timeline, then our engine tries to take care of that as best as possible

  1. Add a PlayableDirector component to a gameobject in your scene
  2. Create a timeline asset
  3. assign the timeline to the playable director
  4. add a audio track to the timeline and drag your audioclip onto the track
  5. add one or multiple animation tracks to the timeline and assign the animations (see docs:
    https://docs.unity3d.com/Packages/com.unity.timeline@1.8/manual/wf_instance.html)

You could have a look at the scrollytelling scene from our samples to see a simple reference (or import the timeline samples from unityā€™s timeline package via the package manager window)

Brilliant! Huge thanks, as always. That all sounds absolutely ideal - Iā€™ll get right on it :slightly_smiling_face::+1:

by user 908977119781060648

Just an idle/curious thoughtā€¦ :thought_balloon: :thought_balloon: :thought_balloon: :thinking:

Given that, at the moment, the audio begins playing when a user interacts with the content without me having to add a start button or any extra controllers or timelines anything, is there a reason why I canā€™t ā€˜attachā€™ my animation to whatever is making that happen?

i.e. thereā€™s obviously some sort of ā€˜start on user interactionā€™ thing in already existence, so might it be possible to somehow ā€˜piggy-backā€™ on that rather than introduce additional stuff? :thought_balloon: :thought_balloon: :thought_balloon: Or perhaps that aspect of things isnā€™t related to Unity or Needle?

Not a deal-breakerā€¦ Iā€™m just musing :slightly_smiling_face:

by user 908977119781060648

You could of course hook into e.g. the AudioSource.audio (which is the threejs SpatialAudio class i think) and use the events to start and update your animation manually too and try to keep it in sync

Ooooooo, rightā€¦ Iā€™ll have a look.

It sounds a lot less elegant than your earlier suggestions, though, so probably not a great way to go. It was just curiosity, really :slightly_smiling_face:

by user 908977119781060648

You can totally do that! E.g. there could be a character with an idle animation waving at you, and once you do anything (click, drag, interact) another animation + synced audio plays

Okay, youā€™ve got me thinking about it more seriously, now :thinking: :slightly_smiling_face: :thought_balloon: :thought_balloon: :thought_balloon:

by user 908977119781060648

Lots to experiment with :grinning:

by user 908977119781060648

Okay, so despite several days (and nights) wrestling with this Iā€™m really not getting anywhere with my hoped-for outcome.

THE DISCLAIMER :wink:
It should be noted that the failings Iā€™m experiencing are down to my own ineptitude, not Unity or Needle problems.

**THE UPS & DOWNS **
Iā€™ve successfully created a PlayableDirector with an associated timeline asset - and when I set it to ā€˜Play On Awakeā€™ the timeline runs as anticipated. So far, so good :grin: :partying_face:

However, what I need it to do (and what Iā€™m singularly failing to achieve) is for it to only begin playing on user interaction.

Iā€™ve unchecked ā€˜Play On Awakeā€™ in the PlayableDirector (which successfully stops the timeline from playing automatically) but have been unable to find a way to make it start playing when required :neutral_face:

As mentioned previously, in an ideal world what Iā€™d really like is for the timeline to be triggered to play as soon as the user begins exploring the scene (via the orbit controls, for example).

However, I havenā€™t had any luck trying to find general info about how to go about achieving that. So, instead, Iā€™ve tried to trigger it via a buttonā€¦ but Iā€™m obviously doing something terribly wrong as none of the various, and undoubtedly messy, permutations that Iā€™ve concocted make it run :grimacing:

by user 908977119781060648

THE PLEA FOR HELP
So, basically Iā€™m trying to find a simple, straightforward walkthrough (if one exists) describing how to make either of the following happen:-

1). The PlayableDirector with its associated timeline starts playing as soon as thereā€™s a user interaction (preferred).

2). The PlayableDirector with its associated timeline starts playing as soon as a button is clicked (happy to go with that if option 1 isnā€™t viable).

THE SCRUTINY OF MY VERY SOUL :scream:
I have a horrible sinking feeling that all my troubles and woes can be placed firmly in the ā€˜I donā€™t know anything about Unityā€™ category. A problem compounded by the fact that I find Unity seems to insist on even the simplest of tasks being an exercise in labyrinthine torture; something which has made me reluctant to pursue it much in the past.

ISNā€™T THERE ANYTHING FASTER THAN A MICROWAVE?
At nearly 60 years oldā€¦ so, with time being in rather shorter supply than it was when I was younger :rofl: ā€¦ I have a relatively recently acquired predilection for trying to cut straight to the chase. Sigh. I guess thatā€™ll have to change and Iā€™ll just have to knuckle back down and do the work :grimacing:

by user 908977119781060648

Hello @frannie :beer: can you disabling play on awake and add a tiny script that does something like this: if(this.context.input.getPointerDown(0)) timeline.play() ?

Hi @marcel :cactus: thanks for the suggestionā€¦ Iā€™ll give that a goā€¦

by user 908977119781060648

Also thank you for that detailed message! Weā€™ll figure this out together :slightly_smiling_face:

Hmmmmmmā€¦ it doesnā€™t like something about my latest efforts. Iā€™m now being repeatedly prompted to redo the export setup each time I make any changes. So, Iā€™m going to ditch this test project and make a new version. Iā€™ll pop back with more info when I get sorted :slightly_smiling_face:

by user 908977119781060648

Sadly, no joy thus far today.

Iā€™ve made several new test projects (all collab sandboxes) to try the above suggestion in a fresh environment, but each time I get the following (attached)

by user 908977119781060648

by user 908977119781060648

Sorry, hereā€™s the more comprehensive pic that I meant to postā€¦

by user 908977119781060648