123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #if UNITY_5_4_OR_NEWER || (UNITY_5 && !UNITY_5_0)
- #define UNITY_HELPATTRIB
- #endif
- using UnityEngine;
- //-----------------------------------------------------------------------------
- // Copyright 2015-2020 RenderHeads Ltd. All rights reserved.
- //-----------------------------------------------------------------------------
- namespace RenderHeads.Media.AVProVideo
- {
- /// <summary>
- /// This is an experimental feature and only works in Windows currently
- /// Audio is grabbed from the MediaPlayer and rendered via Unity
- /// This allows audio to have 3D spatial control, effects applied and to be spatialised for VR
- /// </summary>
- [RequireComponent(typeof(AudioSource))]
- [AddComponentMenu("AVPro Video/Audio Output", 400)]
- #if UNITY_HELPATTRIB
- [HelpURL("http://renderheads.com/products/avpro-video/")]
- #endif
- public class AudioOutput : MonoBehaviour
- {
- public enum AudioOutputMode
- {
- Single,
- Multiple
- }
- public AudioOutputMode _audioOutputMode = AudioOutputMode.Multiple;
- [SerializeField]
- private MediaPlayer _mediaPlayer;
- private AudioSource _audioSource;
- [HideInInspector]
- public int _channelMask = -1;
- void Awake()
- {
- _audioSource = this.GetComponent<AudioSource>();
- }
- void Start()
- {
- ChangeMediaPlayer(_mediaPlayer);
- #if (!UNITY_5 && !UNITY_5_4_OR_NEWER)
- Debug.LogWarning("[AVProVideo] AudioOutput component requires Unity 5.x or above", this);
- #endif
- }
- void OnDestroy()
- {
- ChangeMediaPlayer(null);
- }
- void Update()
- {
- if (_mediaPlayer != null && _mediaPlayer.Control != null && _mediaPlayer.Control.IsPlaying())
- {
- ApplyAudioSettings(_mediaPlayer, _audioSource);
- }
- }
- public void ChangeMediaPlayer(MediaPlayer newPlayer)
- {
- // When changing the media player, handle event subscriptions
- if (_mediaPlayer != null)
- {
- _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
- _mediaPlayer = null;
- }
- _mediaPlayer = newPlayer;
- if (_mediaPlayer != null)
- {
- _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
- }
- }
- // Callback function to handle events
- private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
- {
- switch (et)
- {
- case MediaPlayerEvent.EventType.Closing:
- _audioSource.Stop();
- break;
- case MediaPlayerEvent.EventType.Started:
- ApplyAudioSettings(_mediaPlayer, _audioSource);
- _audioSource.Play();
- break;
- }
- }
- private static void ApplyAudioSettings(MediaPlayer player, AudioSource audioSource)
- {
- // Apply volume and mute from the MediaPlayer to the AudioSource
- if (player != null && player.Control != null)
- {
- float volume = player.Control.GetVolume();
- bool isMuted = player.Control.IsMuted();
- float rate = player.Control.GetPlaybackRate();
- audioSource.volume = volume;
- audioSource.mute = isMuted;
- audioSource.pitch = rate;
- }
- }
- #if (UNITY_5 || UNITY_5_4_OR_NEWER)
- #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
- void OnAudioFilterRead(float[] data, int channels)
- {
- AudioOutputManager.Instance.RequestAudio(this, _mediaPlayer, data, _channelMask, channels, _audioOutputMode);
- }
- #endif
- #endif
- }
- }
|