AudioOutput.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0)
  2. #define UNITY_HELPATTRIB
  3. #endif
  4. using UnityEngine;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2015-2020 RenderHeads Ltd. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace RenderHeads.Media.AVProVideo
  9. {
  10. /// <summary>
  11. /// This is an experimental feature and only works in Windows currently
  12. /// Audio is grabbed from the MediaPlayer and rendered via Unity
  13. /// This allows audio to have 3D spatial control, effects applied and to be spatialised for VR
  14. /// </summary>
  15. [RequireComponent(typeof(AudioSource))]
  16. [AddComponentMenu("AVPro Video/Audio Output", 400)]
  17. #if UNITY_HELPATTRIB
  18. [HelpURL("http://renderheads.com/products/avpro-video/")]
  19. #endif
  20. public class AudioOutput : MonoBehaviour
  21. {
  22. public enum AudioOutputMode
  23. {
  24. Single,
  25. Multiple
  26. }
  27. public AudioOutputMode _audioOutputMode = AudioOutputMode.Multiple;
  28. [SerializeField]
  29. private MediaPlayer _mediaPlayer;
  30. private AudioSource _audioSource;
  31. [HideInInspector]
  32. public int _channelMask = -1;
  33. void Awake()
  34. {
  35. _audioSource = this.GetComponent<AudioSource>();
  36. }
  37. void Start()
  38. {
  39. ChangeMediaPlayer(_mediaPlayer);
  40. #if (!UNITY_5 && !UNITY_5_4_OR_NEWER)
  41. Debug.LogWarning("[AVProVideo] AudioOutput component requires Unity 5.x or above", this);
  42. #endif
  43. }
  44. void OnDestroy()
  45. {
  46. ChangeMediaPlayer(null);
  47. }
  48. void Update()
  49. {
  50. if (_mediaPlayer != null && _mediaPlayer.Control != null && _mediaPlayer.Control.IsPlaying())
  51. {
  52. ApplyAudioSettings(_mediaPlayer, _audioSource);
  53. }
  54. }
  55. public void ChangeMediaPlayer(MediaPlayer newPlayer)
  56. {
  57. // When changing the media player, handle event subscriptions
  58. if (_mediaPlayer != null)
  59. {
  60. _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
  61. _mediaPlayer = null;
  62. }
  63. _mediaPlayer = newPlayer;
  64. if (_mediaPlayer != null)
  65. {
  66. _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
  67. }
  68. }
  69. // Callback function to handle events
  70. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  71. {
  72. switch (et)
  73. {
  74. case MediaPlayerEvent.EventType.Closing:
  75. _audioSource.Stop();
  76. break;
  77. case MediaPlayerEvent.EventType.Started:
  78. ApplyAudioSettings(_mediaPlayer, _audioSource);
  79. _audioSource.Play();
  80. break;
  81. }
  82. }
  83. private static void ApplyAudioSettings(MediaPlayer player, AudioSource audioSource)
  84. {
  85. // Apply volume and mute from the MediaPlayer to the AudioSource
  86. if (player != null && player.Control != null)
  87. {
  88. float volume = player.Control.GetVolume();
  89. bool isMuted = player.Control.IsMuted();
  90. float rate = player.Control.GetPlaybackRate();
  91. audioSource.volume = volume;
  92. audioSource.mute = isMuted;
  93. audioSource.pitch = rate;
  94. }
  95. }
  96. #if (UNITY_5 || UNITY_5_4_OR_NEWER)
  97. #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN || UNITY_WSA_10_0 || UNITY_WINRT_8_1 || UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS || UNITY_TVOS
  98. void OnAudioFilterRead(float[] data, int channels)
  99. {
  100. AudioOutputManager.Instance.RequestAudio(this, _mediaPlayer, data, _channelMask, channels, _audioOutputMode);
  101. }
  102. #endif
  103. #endif
  104. }
  105. }