123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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<AudioSource>();
- }
- 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<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
- this._mediaPlayer = null;
- }
- this._mediaPlayer = newPlayer;
- if (this._mediaPlayer != null)
- {
- this._mediaPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(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
- }
- }
- }
|