Search Unity

How to get audio clip which is playing at a specific time in timeline ?

Discussion in 'Timeline' started by Sami_AlEsh, May 24, 2021.

  1. Sami_AlEsh

    Sami_AlEsh

    Joined:
    Sep 17, 2018
    Posts:
    5
    I have timeline and a playable asset. in playable asset there is one audio track with multiple audio clips in different timing.

    How to get audio clip which is playing when director's time is 105 for example ?!
     
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    If you have access to the asset, you can use the API to get the TimelineClips from the track asset, and you can go through them to find the clip(s) where T> start && t< (start + duration). You can then cast the asset from the TimelineClip to AudioPlayableAsset to get the AudioClip.

    If this is something you need often, I would suggest that you generate a track that contains this information instead, because querying this often will be expensive.
     
    Sami_AlEsh likes this.
  3. Sami_AlEsh

    Sami_AlEsh

    Joined:
    Sep 17, 2018
    Posts:
    5
    I forgot to update this post. Yup, I have access to the asset and this is exactly what I did.
    But, I didn't understand the second mentioned solution "Generating track that contains this information"?
     
  4. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    The idea would be to parse the track at edit time, and create a new track that contains the information you want.

    Each clip would be just the name of the current audio clip. And evaluating the track would write the name of the current clip to a component.

    That way, you would just need to read the currently playing Audio clip from the component, instead of having your own code that parses the track every frame.