I’m trying to generate timeline audio clips from script. So far I have the following in my TimelinePlayer.cs script (an empty timeline already exists in my scene):
/// Create new track
AudioTrack track = timeline.CreateTrack<AudioTrack>(null, "track 1");
/// Create an audio clip in the track
var audioAsset = (AudioPlayableAsset)(track.CreateClip<AudioPlayableAsset>().asset);
audioAsset.name = "audio asset 1"; /// Not entirely sure if this is necessary
audioAsset.clip = <MY_AUDIO_CLIP>; /// This is an AudioClip type object which works when adding it to a regular AudioSource component not related to the timeline
audioAsset.clip.name = "audio clip 1";
/// Set clip properties
var clip = track.GetClips().FirstOrDefault();
clip.start = 0f;
But in editor play mode, there seems to be no audio source assigned to the track in the timeline.
I cannot figure out how to assign an audio source to an AudioTrack. Can someone help me understand how to do this?
What was missing was that I needed to put an AudioSource in my scene. I load the clip to this audio source, then reference it from the track in the timeline.
/// Load audio source (as a component of a prefab named "AudioPlayer" in this case)
GameObject audioPlayerObj = Instantiate(Resources.Load("prefabs/AudioPlayer") as GameObject);
audioPlayerObj.name = "track 1";
AudioSource audioSource = audioPlayerObj.GetComponent<AudioSource>();
audioSource.clip = <MY_AUDIO_CLIP>;
audioSource.playOnAwake = false;
audioSource.name = "track 1";
audioSource.volume = 1f;
/// Create new track
AudioTrack track = timeline.CreateTrack<AudioTrack>(null, "track 1");
/// Create an audio clip in the track
var audioAsset = (AudioPlayableAsset)(track.CreateClip<AudioPlayableAsset>().asset);
audioAsset.name = "audio asset 1"; /// Not entirely sure if this is necessary
audioAsset.clip = <MY_AUDIO_CLIP>; /// This is an AudioClip type object which works when adding it to a regular AudioSource component not related to the timeline
audioAsset.clip.name = "audio clip 1";
/// Set clip properties
var clip = track.GetClips().FirstOrDefault();
clip.start = 0f;
/// Bind the audio clip to the audio player gameobject
/// Note: this seems to be optional, but without doing this, the Audio Source field in your timeline track will still say "None" as pictured in my original post
director.SetReferenceValue("track 1", audioSource.gameObject);
director.SetGenericBinding(track, audioSource);
audioPlayers.Add("track 1", audioSource);