using System;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo.Demos
{
	public class ChangeAudioTrack : MonoBehaviour
	{
		private void OnEnable()
		{
			this._isQueued = true;
		}

		private void Update()
		{
			if (this._isQueued && this.IsVideoLoaded())
			{
				ChangeAudioTrack.DoChangeAudioTrack(this._mediaPlayer, this._trackIndex);
				this._isQueued = false;
			}
		}

		private bool IsVideoLoaded()
		{
			return this._mediaPlayer != null && this._mediaPlayer.Info != null && this._mediaPlayer.Control.HasMetaData();
		}

		private static bool DoChangeAudioTrack(MediaPlayer mp, int index)
		{
			bool result = false;
			int audioTrackCount = mp.Info.GetAudioTrackCount();
			if (index >= 0 && index < audioTrackCount)
			{
				mp.Control.SetAudioTrack(index);
				result = true;
			}
			else
			{
				Debug.LogWarning("[AVProVideo] Audio track index is out of range: " + index);
			}
			return result;
		}

		public MediaPlayer _mediaPlayer;

		public int _trackIndex;

		private bool _isQueued;
	}
}
