ChangeAudioTrack.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using UnityEngine;
  3. namespace RenderHeads.Media.AVProVideo.Demos
  4. {
  5. public class ChangeAudioTrack : MonoBehaviour
  6. {
  7. private void OnEnable()
  8. {
  9. this._isQueued = true;
  10. }
  11. private void Update()
  12. {
  13. if (this._isQueued && this.IsVideoLoaded())
  14. {
  15. ChangeAudioTrack.DoChangeAudioTrack(this._mediaPlayer, this._trackIndex);
  16. this._isQueued = false;
  17. }
  18. }
  19. private bool IsVideoLoaded()
  20. {
  21. return this._mediaPlayer != null && this._mediaPlayer.Info != null && this._mediaPlayer.Control.HasMetaData();
  22. }
  23. private static bool DoChangeAudioTrack(MediaPlayer mp, int index)
  24. {
  25. bool result = false;
  26. int audioTrackCount = mp.Info.GetAudioTrackCount();
  27. if (index >= 0 && index < audioTrackCount)
  28. {
  29. mp.Control.SetAudioTrack(index);
  30. result = true;
  31. }
  32. else
  33. {
  34. Debug.LogWarning("[AVProVideo] Audio track index is out of range: " + index);
  35. }
  36. return result;
  37. }
  38. public MediaPlayer _mediaPlayer;
  39. public int _trackIndex;
  40. private bool _isQueued;
  41. }
  42. }