Search Unity

Audio Playables and custom use

Discussion in 'Timeline' started by tsweeney, Jul 20, 2017.

  1. tsweeney

    tsweeney

    Joined:
    Oct 14, 2013
    Posts:
    4
    Most of what I see for examples of building your own use of Playables involve Animation mixing, blending, and so on. Are there any examples of doing similar tricks with Audio? or is the underlying Audio playables work not yet ready for that sort of effort?

    For instance, if I wanted to build my own Playable that faded between two audio clips, how would I go about doing so? I attempted to mimic the Animation example using AudioPlayableOutput, AudioClipPlayable and AudioMixerPlayable, assuming they must work in a similar way, but I'm not actually able to get it to work.

    I've been able to:
    a) get an AudioClipPlayable to play
    b) flip between multiple AudioClipPlayables at the start based on script properties

    But I haven't been able to figure out how to connect multiple AudioClipPlayables and mix between clips in pretty much the same way the Timeline seems to do itself. Maybe I'm not supposed to do that, but I'm interested to know what's going on.
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Here's an example that should help you out, it somewhat mimics how Timeline does it.

    Code (CSharp):
    1.     public class AudioClipPlayableAsset : PlayableAsset
    2.     {
    3.         public AudioClip audioClip;
    4.  
    5.         public AudioClipPlayable audioPlayable1;
    6.         public AudioClipPlayable audioPlayable2;
    7.         public AudioMixerPlayable audioMixer;
    8.  
    9.         public override double duration { get { return kClipDuration * 2; } }
    10.         public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    11.         {
    12.             audioPlayable1 = AudioClipPlayable.Create(graph, audioClip, true);
    13.             audioPlayable2 = AudioClipPlayable.Create(graph, audioClip, true);
    14.             audioMixer = AudioMixerPlayable.Create(graph, 2);
    15.             graph.Connect(audioPlayable1, 0, audioMixer, 0);
    16.             graph.Connect(audioPlayable2, 0, audioMixer, 1);
    17.  
    18.             audioPlayable1.Seek(0, 0, kClipDuration);
    19.             audioPlayable2.Seek(0, kBlendStart, kClipDuration);
    20.             audioMixer.SetInputWeight(0, 1);
    21.             audioMixer.SetInputWeight(1, 1);
    22.  
    23.             var output = AudioPlayableOutput.Create(graph, string.Empty, null);
    24.             output.SetSourcePlayable(audioMixer);
    25.             return audioMixer;
    26.         }
    27.     }
    28.