Access Audio Track Volume

I have an audio track on a timeline. I’d like to be able to change the volume of this track by code. I’ve tried accessing the volume of the audiosource which is attached to it, but this doesn’t change the timeline’s volume. Is there a way to access the volume of a timeline audio track?

Thanks!

No, but it’s something we are looking to add soon.

3 Likes

2019.1 ?

Volume in 2019.1, animated volume in 2019.2.

1 Like

I have taken look at AudioTrack, but still cannot find volume accessor/setter. Using Unity 2019.2.8

assign an Audiosource to the Audiotrack, then control the volume of that Audiosource, either directly or via a mixer.

The volume can be accessed from the inspector, or from the the curve editor. If you select a clip, you can edit the volume in the inspector (but it is not animated, except for using ease/blend in/out).

Is it added?

Yes, from 2019.1 if I recall correctly.

Can you tell me how to use by code?

Unfortunately, the necessary fields don’t seem to in the public API so it can’t be used to set their value directly.

However, you can set an animated value for them using the following:

audioTrack.CreateCurves("nameOfAnimationClip");
audioTrack.curves.SetCurve(string.Empty, typeof(AudioTrack), "volume", AnimationCurve.Linear(0,0,1,1));
audioTrack.curves.SetCurve(string.Empty, typeof(AudioTrack), "stereoPan", AnimationCurve.Linear(0,-1,1,1));
audioTrack.curves.SetCurve(string.Empty, typeof(AudioTrack), "spatialBlend", AnimationCurve.Linear(0,0,1,1));