Gets the number of audio tracks that are embedded in the video clip. (Read Only).
A video clip can contain multiple audio tracks. It can have separate audio tracks for different languages, commentary, accessibility, or to separate music, sound effects and voices. This is useful because you can:
To enable or deactivate a certain audio track from the clip, use VideoPlayer.EnableAudioTrack.
Additional resources: VideoClip.GetAudioLanguage, VideoPlayer.EnableAudioTrack, VideoPlayer.SetDirectAudioMute, VideoPlayer.SetDirectAudioVolume.
using UnityEngine; using UnityEngine.Video;
public class AudioTrackCountExample : MonoBehaviour { VideoPlayer videoPlayer;
int currentTrack;
void Start() { videoPlayer = GetComponent<VideoPlayer>(); VideoClip videoClip = videoPlayer.clip;
// Loop through all tracks and deactivate all except the first. for (ushort i = 1; i < videoClip.audioTrackCount; i++) { videoPlayer.EnableAudioTrack(i, false); } videoPlayer.Play(); }
void Update() { // Press the Spacebar to change audio track. if (Input.GetKeyDown(KeyCode.Space)) { ChangeAudioTrack(); } }
public void ChangeAudioTrack() { // VideoPlayer needs to stop before it can change track. videoPlayer.Stop();
videoPlayer.EnableAudioTrack((ushort)currentTrack, false); currentTrack = (currentTrack + 1) % videoPlayer.audioTrackCount; videoPlayer.EnableAudioTrack((ushort)currentTrack, true);
videoPlayer.Play(); } }