123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using System;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
- namespace RenderHeads.Media.AVProVideo.Demos
- {
- public class VCR : MonoBehaviour
- {
- public MediaPlayer PlayingPlayer
- {
- get
- {
- if (this.LoadingPlayer == this._mediaPlayer)
- {
- return this._mediaPlayerB;
- }
- return this._mediaPlayer;
- }
- }
- public MediaPlayer LoadingPlayer
- {
- get
- {
- return this._loadingPlayer;
- }
- }
- private void SwapPlayers()
- {
- this.PlayingPlayer.Control.Pause();
- if (this.LoadingPlayer == this._mediaPlayer)
- {
- this._loadingPlayer = this._mediaPlayerB;
- }
- else
- {
- this._loadingPlayer = this._mediaPlayer;
- }
- this._mediaDisplay.CurrentMediaPlayer = this.PlayingPlayer;
- }
- public void OnOpenVideoFile()
- {
- this.LoadingPlayer.m_VideoPath = Path.Combine(this._folder, this._videoFiles[this._VideoIndex]);
- this._VideoIndex = (this._VideoIndex + 1) % this._videoFiles.Length;
- if (string.IsNullOrEmpty(this.LoadingPlayer.m_VideoPath))
- {
- this.LoadingPlayer.CloseVideo();
- this._VideoIndex = 0;
- }
- else
- {
- this.LoadingPlayer.OpenVideoFromFile(this._location, this.LoadingPlayer.m_VideoPath, this._AutoStartToggle.isOn);
- }
- }
- public void OnAutoStartChange()
- {
- if (this.PlayingPlayer && this._AutoStartToggle && this._AutoStartToggle.enabled && this.PlayingPlayer.m_AutoStart != this._AutoStartToggle.isOn)
- {
- this.PlayingPlayer.m_AutoStart = this._AutoStartToggle.isOn;
- }
- if (this.LoadingPlayer && this._AutoStartToggle && this._AutoStartToggle.enabled && this.LoadingPlayer.m_AutoStart != this._AutoStartToggle.isOn)
- {
- this.LoadingPlayer.m_AutoStart = this._AutoStartToggle.isOn;
- }
- }
- public void OnMuteChange()
- {
- if (this.PlayingPlayer)
- {
- this.PlayingPlayer.Control.MuteAudio(this._MuteToggle.isOn);
- }
- if (this.LoadingPlayer)
- {
- this.LoadingPlayer.Control.MuteAudio(this._MuteToggle.isOn);
- }
- }
- public void OnPlayButton()
- {
- if (this.PlayingPlayer)
- {
- this.PlayingPlayer.Control.Play();
- }
- }
- public void OnPauseButton()
- {
- if (this.PlayingPlayer)
- {
- this.PlayingPlayer.Control.Pause();
- }
- }
- public void OnVideoSeekSlider()
- {
- if (this.PlayingPlayer && this._videoSeekSlider && this._videoSeekSlider.value != this._setVideoSeekSliderValue)
- {
- this.PlayingPlayer.Control.Seek(this._videoSeekSlider.value * this.PlayingPlayer.Info.GetDurationMs());
- }
- }
- public void OnVideoSliderDown()
- {
- if (this.PlayingPlayer)
- {
- this._wasPlayingOnScrub = this.PlayingPlayer.Control.IsPlaying();
- if (this._wasPlayingOnScrub)
- {
- this.PlayingPlayer.Control.Pause();
- }
- this.OnVideoSeekSlider();
- }
- }
- public void OnVideoSliderUp()
- {
- if (this.PlayingPlayer && this._wasPlayingOnScrub)
- {
- this.PlayingPlayer.Control.Play();
- this._wasPlayingOnScrub = false;
- }
- }
- public void OnAudioVolumeSlider()
- {
- if (this.PlayingPlayer && this._audioVolumeSlider && this._audioVolumeSlider.value != this._setAudioVolumeSliderValue)
- {
- this.PlayingPlayer.Control.SetVolume(this._audioVolumeSlider.value);
- }
- if (this.LoadingPlayer && this._audioVolumeSlider && this._audioVolumeSlider.value != this._setAudioVolumeSliderValue)
- {
- this.LoadingPlayer.Control.SetVolume(this._audioVolumeSlider.value);
- }
- }
- public void OnRewindButton()
- {
- if (this.PlayingPlayer)
- {
- this.PlayingPlayer.Control.Rewind();
- }
- }
- private void Awake()
- {
- this._loadingPlayer = this._mediaPlayerB;
- }
- private void Start()
- {
- if (this.PlayingPlayer)
- {
- this.PlayingPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnVideoEvent));
- if (this.LoadingPlayer)
- {
- this.LoadingPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnVideoEvent));
- }
- if (this._audioVolumeSlider && this.PlayingPlayer.Control != null)
- {
- float volume = this.PlayingPlayer.Control.GetVolume();
- this._setAudioVolumeSliderValue = volume;
- this._audioVolumeSlider.value = volume;
- }
- this._AutoStartToggle.isOn = this.PlayingPlayer.m_AutoStart;
- if (this.PlayingPlayer.m_AutoOpen)
- {
- }
- this.OnOpenVideoFile();
- }
- }
- private void Update()
- {
- if (this.PlayingPlayer && this.PlayingPlayer.Info != null && this.PlayingPlayer.Info.GetDurationMs() > 0f)
- {
- float currentTimeMs = this.PlayingPlayer.Control.GetCurrentTimeMs();
- float num = currentTimeMs / this.PlayingPlayer.Info.GetDurationMs();
- this._setVideoSeekSliderValue = num;
- this._videoSeekSlider.value = num;
- }
- }
- public void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
- {
- switch (et)
- {
- case MediaPlayerEvent.EventType.FirstFrameReady:
- this.SwapPlayers();
- break;
- }
- Debug.Log("Event: " + et.ToString());
- }
- public MediaPlayer _mediaPlayer;
- public MediaPlayer _mediaPlayerB;
- public DisplayUGUI _mediaDisplay;
- public Slider _videoSeekSlider;
- private float _setVideoSeekSliderValue;
- private bool _wasPlayingOnScrub;
- public Slider _audioVolumeSlider;
- private float _setAudioVolumeSliderValue;
- public Toggle _AutoStartToggle;
- public Toggle _MuteToggle;
- public MediaPlayer.FileLocation _location = MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder;
- public string _folder = "AVProVideoDemos/";
- public string[] _videoFiles = new string[]
- {
- "BigBuckBunny_720p30.mp4",
- "SampleSphere.mp4"
- };
- private int _VideoIndex;
- private MediaPlayer _loadingPlayer;
- }
- }
|