AudioOutput.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. namespace RenderHeads.Media.AVProVideo
  5. {
  6. [RequireComponent(typeof(AudioSource))]
  7. [AddComponentMenu("AVPro Video/Audio Output", 400)]
  8. [HelpURL("http://renderheads.com/product/avpro-video/")]
  9. public class AudioOutput : MonoBehaviour
  10. {
  11. private void Awake()
  12. {
  13. this._audioSource = base.GetComponent<AudioSource>();
  14. }
  15. private void Start()
  16. {
  17. this.ChangeMediaPlayer(this._mediaPlayer);
  18. }
  19. private void OnDestroy()
  20. {
  21. this.ChangeMediaPlayer(null);
  22. }
  23. private void Update()
  24. {
  25. if (this._mediaPlayer != null && this._mediaPlayer.Control != null && this._mediaPlayer.Control.IsPlaying())
  26. {
  27. AudioOutput.ApplyAudioSettings(this._mediaPlayer, this._audioSource);
  28. }
  29. }
  30. public void ChangeMediaPlayer(MediaPlayer newPlayer)
  31. {
  32. if (this._mediaPlayer != null)
  33. {
  34. this._mediaPlayer.Events.RemoveListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
  35. this._mediaPlayer = null;
  36. }
  37. this._mediaPlayer = newPlayer;
  38. if (this._mediaPlayer != null)
  39. {
  40. this._mediaPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
  41. }
  42. }
  43. private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  44. {
  45. if (et != MediaPlayerEvent.EventType.Closing)
  46. {
  47. if (et == MediaPlayerEvent.EventType.Started)
  48. {
  49. AudioOutput.ApplyAudioSettings(this._mediaPlayer, this._audioSource);
  50. this._audioSource.Play();
  51. }
  52. }
  53. else
  54. {
  55. this._audioSource.Stop();
  56. }
  57. }
  58. private static void ApplyAudioSettings(MediaPlayer player, AudioSource audioSource)
  59. {
  60. if (player != null && player.Control != null)
  61. {
  62. float volume = player.Control.GetVolume();
  63. bool mute = player.Control.IsMuted();
  64. float playbackRate = player.Control.GetPlaybackRate();
  65. audioSource.volume = volume;
  66. audioSource.mute = mute;
  67. audioSource.pitch = playbackRate;
  68. }
  69. }
  70. private void OnAudioFilterRead(float[] data, int channels)
  71. {
  72. AudioOutputManager.Instance.RequestAudio(this, this._mediaPlayer, data, this._channelMask, channels, this._audioOutputMode);
  73. }
  74. public AudioOutput.AudioOutputMode _audioOutputMode = AudioOutput.AudioOutputMode.Multiple;
  75. [SerializeField]
  76. private MediaPlayer _mediaPlayer;
  77. private AudioSource _audioSource;
  78. [HideInInspector]
  79. public int _channelMask = -1;
  80. public enum AudioOutputMode
  81. {
  82. Single,
  83. Multiple
  84. }
  85. }
  86. }