Search Unity

Access AudioClip in timeline with code

Discussion in 'Timeline' started by carlpetersson, Apr 26, 2018.

  1. carlpetersson

    carlpetersson

    Joined:
    Aug 29, 2017
    Posts:
    7
    I am working on a project where I need a variable to contain the AudioClip I have added on my timeline. Since the AudioSource that I am using in my timeline does now show the AudioClip playing when the application is running I am wondering how I can refer to this clip within my code in any other way. Hopefully this question make sense otherwise I will be happy trying to explain my problem more.

    Thanks in advance!
     
  2. vladala

    vladala

    Unity Technologies

    Joined:
    Mar 3, 2017
    Posts:
    189
    The TimelineClip returns the asset, which is of type AudioPlayableAsset for audio tracks, which in turn contain a reference to the AudioClip:
    The ((TimelineClip.asset) as AudioPlayableAsset).clip

    Is this what you were looking for?
     
  3. carlpetersson

    carlpetersson

    Joined:
    Aug 29, 2017
    Posts:
    7
    Thanks for your response!

    I have played around a little with the information you gave me but I cannot seem to understand how to tie that to my timeline. I want to access the name of the audioclip on a timeline like the one you can see on the image. Am I able to do this with the code that you provided?

    Thanks

    upload_2018-4-27_8-4-41.png
     
  4. vladala

    vladala

    Unity Technologies

    Joined:
    Mar 3, 2017
    Posts:
    189
    There is an easier version for that:
    TimelineClip has a property called displayName . This is what's drawn in the UI (and you can rename the clip from the inspector).
     
  5. carlpetersson

    carlpetersson

    Joined:
    Aug 29, 2017
    Posts:
    7
    I guess what I don't get is how you get the TimlineClip properties to refer to my specific timeline. How do I get the properties in TimelineClip occupied with my timeline data so that I can refer to them in code?
     
  6. carlpetersson

    carlpetersson

    Joined:
    Aug 29, 2017
    Posts:
    7
    Still trying to figure out how to access the AudioSource and AudioClip inside my timeline. Is there maybe something like a get() function that will allow me to access the properties inside my timeline? Appreciate any help I can get
     
  7. sebastienlh

    sebastienlh

    Unity Technologies

    Joined:
    Sep 22, 2017
    Posts:
    40
    If I understand correctly, you want to get to the contents of a clip in a Timeline. If so, there are a few steps to do this and you'll need a reference to the PlayableDirector component that contains your Timeline. Then, there is a bit of gymnastics involved; here's an example:

    Code (CSharp):
    1. var director = GetComponent<PlayableDirector>();
    2. var timeline = (TimelineAsset) director.playableAsset;
    3. var audioTrack = timeline.GetOutputTracks().FirstOrDefault(t => t is AudioTrack); // If you give your track a custom name, you can use that instead
    4. var clip = audioTrack.clips.FirstOrDefault();
    5. var audioClip = ((AudioPlayableAsset) clip.asset).clip; // This is your AudioClip
    6.  
    7. Debug.LogFormat("audioClip = {0} ({1})", audioClip.name, audioClip.GetType().FullName);
    This code will get the first AudioClip of the first AudioTrack in your Timeline. You should probably add a few null-checks to this code if you want to use it in production.

    Does that help?


    Edit: I had `flattenedTracks` in the snippet before, instead of `GetOutputTracks()` which was a mistake.
     
    Last edited: May 3, 2018
    steril likes this.
  8. carlpetersson

    carlpetersson

    Joined:
    Aug 29, 2017
    Posts:
    7
    On line 3 I am getting an error message for flattenedTracks that is saying that TimelineAsset does not contain a definition or extension method called flattenedTracks. I thought I was missing a library that I cannot define but that might not be it?
     
  9. carlpetersson

    carlpetersson

    Joined:
    Aug 29, 2017
    Posts:
    7
    So I have this function that has an AudioClip as one of its variables (void function(AudioClip soundFile)) like the example and I want to use this function with a variable containing the AudioClip referring to the one I currently have in my timeline called Ivarro-Grind... as you can see in an earlier post. I probably should have mentioned this explanation in the beginning but oh well. Thanks for taking your time trying to help me out tho, I appreciate it.
     
  10. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    You can use timelineAsset.GetOutputTracks().FirstOrDefault(t => t is AudioTrack) instead.
     
  11. carlpetersson

    carlpetersson

    Joined:
    Aug 29, 2017
    Posts:
    7
    Sorry for a late response but I just wanted to say thanks to everyone that took there time and helped me out! Because of that I was able to figure it out and make it work.
     
    seant_unity likes this.
  12. cobykaufer

    cobykaufer

    Joined:
    Aug 8, 2018
    Posts:
    6