- Home /
How to stop Audio Track in Timeline?
I've tried finding the AudioSource which the Timeline creates to play it's sound (even when there's a reference in the TimelineAsset) when started through the PlayableDirector's Play function, without any success. I've tried FindObjectsOfType/Resources.FindObjectsOfTpyeAll, neither will find it. Setting the Time.timeScale won't stop a started AudioTrack from playing through the Timeline. Setting the Timeline's PlayableGraph's speed (suggested by a Unity forum employee in another post to "Pause" the timeline) won't stop it either. Pausing the Timeline won't either. Basically I've ran out of ways I could find to "Pause" the Timeline in different ways, and all of them would continue to play the AudioClip that has already started from the AudioTrack. I would welcome a way where I can find a reference to the AudioSource that the Timeline made, but so far I didn't have any luck with it.
No, I haven't. I ended up just simply not allowing pausing while audio was playing through the timeline and keep trying to avoid using it for that end. Alas, I'm stuck with a rather old Unity version with my project due to some asset compatibility issues, so I wouldn't know if the Timeline has been improved on since.
Yeah I don't think it's been fixed yet in the later versions either. For some reason it looks like like the Audiosource you assign to it in the timeline bypasses the Audiolistener so Audiolistener.Pause doesn't work.
Answer by roman_sedition · Nov 08, 2018 at 08:20 AM
@Afthrast Ok well I've found one solution around this. It requires that the timeline tracks have an audiosource assigned to it for this to work. After you pause the sounds you normally can with Audiolistner.pause = true you find out what audio is currently being played on the timeline and then you turn the volume to zero (that is the first part of the band-aid fix to stop the current sounds from playing), then after that you use the current time of the timeline playing to figure out the offsets of the clips that were playing, you then reassign those clips to the Audiosources that were silenced and play them again and change their Audiosource.time to what the offset was. In your unpause method you then go through a list of Audiosources you have silenced and revert their volumes back to 1.
Here is some code that I came up with
private List<AudioSource> audioPlayers;//This is mainly for use in your unpause method
//Call this method in your pause method
private void PauseTimelineAudio(PlayableDirector _director)
{
audioPlayers.Clear();
var timelineAsset = _director.playableAsset as TimelineAsset;
double pauseTime = _director.time;
foreach (var track in timelineAsset.GetOutputTracks())
{
AudioTrack pausedAudioTrack = track as AudioTrack;
if (pausedAudioTrack == null)
{
continue;
}
IEnumerable<TimelineClip> trackList = pausedAudioTrack.GetClips();
foreach (TimelineClip timeClip in trackList)
{
if (pauseTime >= timeClip.start && pauseTime <= timeClip.end)
{
foreach (var playableAssetOutput in _director.playableAsset.outputs)
{
foreach (var clipOutput in timeClip.parentTrack.outputs)
{
if(playableAssetOutput.sourceObject == clipOutput.sourceObject)
{
AudioSource audio = _director.GetGenericBinding(clipOutput.sourceObject) as AudioSource;
audioPlayers.Add(audio);
if(audio == null)
{
continue;
}
audio.volume = 0;
AudioPlayableAsset ac = timeClip.asset as AudioPlayableAsset;
pausedClip = ac.clip;
resumeTime = (pauseTime - timeClip.start);
audio.clip = pausedClip;
audio.time = (float)resumeTime;
audio.Play();
}
}
}
}
}
}
}
//In your unpause method, paste this to re-volumize the audiosources that were silenced
foreach(AudioSource sound in audioPlayers)
{
if(sound != null)
{
sound.volume = 1;
}
}
I wasn't able to test it yet and sadly won't be around for a while to do so, so if anyone can confirm this works, I'd be happy to mark it as an answer for those, who find this helpful. As for me, if it requires the timeline track to have an audiosource assigned to it, it won't be an option for me sadly, as I need to assign the playable asset to the director at runtime (depending on a choice that the player makes), I use the director's Play() to start a timeline. With simply selecting the timeline asset, there is no such option as to assign an audiosource to it, only after assigning it in the director's reference. I was mainly interested if I was missing something, because it would've made sense to me, that the director itself would have to contain the audiosource it creates, when it's not assigned (since it manages to stop it when the timeline finishes part of the audio it's supposed to play), and being able to reach it would've made sense to me, but I wasn't able to find a way to get the reference to it.
Your answer
Follow this Question
Related Questions
Timeline AudioClip ease not working 1 Answer
create a pause state and menu that still allows animations 0 Answers
How to Pause game 1 Answer
Playing an audio clip 4 Answers