using System; using UnityEngine; using UnityEngine.Events; namespace RenderHeads.Media.AVProVideo { [RequireComponent(typeof(AudioSource))] [AddComponentMenu("AVPro Video/Audio Output", 400)] [HelpURL("http://renderheads.com/product/avpro-video/")] public class AudioOutput : MonoBehaviour { private void Awake() { this._audioSource = base.GetComponent(); } private void Start() { this.ChangeMediaPlayer(this._mediaPlayer); } private void OnDestroy() { this.ChangeMediaPlayer(null); } private void Update() { if (this._mediaPlayer != null && this._mediaPlayer.Control != null && this._mediaPlayer.Control.IsPlaying()) { AudioOutput.ApplyAudioSettings(this._mediaPlayer, this._audioSource); } } public void ChangeMediaPlayer(MediaPlayer newPlayer) { if (this._mediaPlayer != null) { this._mediaPlayer.Events.RemoveListener(new UnityAction(this.OnMediaPlayerEvent)); this._mediaPlayer = null; } this._mediaPlayer = newPlayer; if (this._mediaPlayer != null) { this._mediaPlayer.Events.AddListener(new UnityAction(this.OnMediaPlayerEvent)); } } private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode) { if (et != MediaPlayerEvent.EventType.Closing) { if (et == MediaPlayerEvent.EventType.Started) { AudioOutput.ApplyAudioSettings(this._mediaPlayer, this._audioSource); this._audioSource.Play(); } } else { this._audioSource.Stop(); } } private static void ApplyAudioSettings(MediaPlayer player, AudioSource audioSource) { if (player != null && player.Control != null) { float volume = player.Control.GetVolume(); bool mute = player.Control.IsMuted(); float playbackRate = player.Control.GetPlaybackRate(); audioSource.volume = volume; audioSource.mute = mute; audioSource.pitch = playbackRate; } } private void OnAudioFilterRead(float[] data, int channels) { AudioOutputManager.Instance.RequestAudio(this, this._mediaPlayer, data, this._channelMask, channels, this._audioOutputMode); } public AudioOutput.AudioOutputMode _audioOutputMode = AudioOutput.AudioOutputMode.Multiple; [SerializeField] private MediaPlayer _mediaPlayer; private AudioSource _audioSource; [HideInInspector] public int _channelMask = -1; public enum AudioOutputMode { Single, Multiple } } }