Search Unity

Changing audio clip at runtime

Discussion in 'Timeline' started by matt_unity455, Jan 13, 2020.

  1. matt_unity455

    matt_unity455

    Joined:
    Sep 25, 2019
    Posts:
    5
    Hi all,

    I'd like to be able to change the audio clip that is played on my audio track at runtime. Have seen some examples on the forums but they are all for older versions of the API and it seems to have undergone some fairly signficant changes.

    Has anyone got any example code they could share, or point me in the right direction?

    Cheers
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,134
    Is this for timeline? The API hasn't changed all that much.

    If you have the timeline clip, and the playable director that plays the timeline:

    ((AudioPlayableAsset)timelineClip.asset).clip = newAudioClip;
    playableDirector.RebuildGraph();
     
  3. matt_unity455

    matt_unity455

    Joined:
    Sep 25, 2019
    Posts:
    5
    Thanks Sean - I actually think my issue is getting hold of the clip in the first place though, not really sure how to grab it.
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,134
    The main way to do that is to iterate through the tracks (TimelineAsset.GetOutputTracks()) for the track you want. Giving the track a particular name is probably the most effective way to find it. Then iterate over the clips (TrackAsset.GetClips()).
     
  5. matt_unity455

    matt_unity455

    Joined:
    Sep 25, 2019
    Posts:
    5
    Thank you Sean, appreciated!
     
unityunity