Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Changing audio clip on timeline at runtime

Discussion in 'Timeline' started by Suduckgames, Jun 6, 2018.

  1. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    147
    Hi, I have a game where the user can choose the voice of their character, so I will like to change the clip of the timeline at runtime.

    I tried it with this code
    Code (CSharp):
    1. .
    2.  var timelineAsset = director.playableAsset as TimelineAsset;
    3.        
    4.  
    5.         foreach (var track in timelineAsset.GetOutputTracks())
    6.         {
    7.             AudioTrack animTrack = track as AudioTrack;
    8.    
    9.             if (animTrack == null)
    10.                 continue;
    11.  
    12.             var binding = director.GetGenericBinding(animTrack);
    13.             if (binding == null)
    14.                 continue;
    15.  
    16.  
    17.             var animator = binding as AudioSource;
    18.             var gameObject = binding as GameObject;
    19.             if (animator == null && gameObject != null)
    20.                 animator = gameObject.GetComponent<AudioSource>();
    21.  
    22.             if (animator == null)
    23.                 continue;
    24.             animator.clip = clip;
    25.  
    26.         }
    27.         director.Evaluate();
    28.  
    But this change the clip of the AudioSource but no the clip of the timeline.

    How I can achieve that?

    Thanks in advance
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,038
    If the director was evaluated prior, or was already playing, then director.RebuildGraph() is needed to apply the changes.
     
  3. Suduckgames

    Suduckgames

    Joined:
    Nov 28, 2016
    Posts:
    147
    Thanks for the reply, I found the problem. I was changing the clip of the AudioSource and rebuilding the the Graph but the clip in the timeline is associated to the timeline instead of the audio source. so I need to change the clip of the AudioPlayableAsset instead of the AudioSource

    Code (CSharp):
    1.         var timelineAsset = director.playableAsset as TimelineAsset;
    2.  
    3.  
    4.         foreach (var track in timelineAsset.GetOutputTracks())
    5.         {
    6.             AudioTrack animTrack = track as AudioTrack;
    7.          
    8.             if (animTrack == null)
    9.                 continue;
    10.             IEnumerable<TimelineClip> lista = animTrack.GetClips();
    11.             foreach (TimelineClip timeClip in lista)
    12.             {
    13.                 AudioPlayableAsset ac = timeClip.asset as AudioPlayableAsset;
    14.                 ac.clip = clip;
    15.             }
    16.  
    17.         }
    18.         director.Evaluate();
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,038
    Oh, right. Of course! Yes, the audio source is mostly used for the transform component (for 3D), the audio track clips contains the actual AudioClips.