using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Events; namespace RenderHeads.Media.AVProVideo.Demos { public class SimpleController : MonoBehaviour { private void Start() { this._mediaPlayer.Events.AddListener(new UnityAction(this.OnMediaPlayerEvent)); } public void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode) { switch (et) { case MediaPlayerEvent.EventType.MetaDataReady: this.GatherProperties(); break; } this.AddEvent(et); } private void AddEvent(MediaPlayerEvent.EventType et) { Debug.Log("[SimpleController] Event: " + et.ToString()); this._eventLog.Enqueue(et.ToString()); if (this._eventLog.Count > 5) { this._eventLog.Dequeue(); this._eventTimer = 1f; } } private void GatherProperties() { if (this._mediaPlayer != null && this._mediaPlayer.Info != null) { this._width = this._mediaPlayer.Info.GetVideoWidth(); this._height = this._mediaPlayer.Info.GetVideoHeight(); this._durationSeconds = this._mediaPlayer.Info.GetDurationMs() / 1000f; } } private void Update() { if (!this._useFading && this._display != null && this._display._mediaPlayer != null && this._display._mediaPlayer.Control != null) { this._display._color = Color.white; this._display._mediaPlayer.Control.SetVolume(1f); } if (this._eventLog != null && this._eventLog.Count > 0) { this._eventTimer -= Time.deltaTime; if (this._eventTimer < 0f) { this._eventLog.Dequeue(); this._eventTimer = 1f; } } } private void LoadVideo(string filePath, bool url = false) { if (!url) { this._nextVideoLocation = MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder; } else { this._nextVideoLocation = MediaPlayer.FileLocation.AbsolutePathOrURL; } this._nextVideoPath = filePath; if (!this._useFading) { if (!this._mediaPlayer.OpenVideoFromFile(this._nextVideoLocation, this._nextVideoPath, this._mediaPlayer.m_AutoStart)) { Debug.LogError("Failed to open video!"); } } else { base.StartCoroutine("LoadVideoWithFading"); } } private static bool VideoIsReady(MediaPlayer mp) { return mp != null && mp.TextureProducer != null && mp.TextureProducer.GetTextureFrameCount() <= 0; } private static bool AudioIsReady(MediaPlayer mp) { return mp != null && mp.Control != null && mp.Control.CanPlay() && mp.Info.HasAudio() && !mp.Info.HasVideo(); } private IEnumerator LoadVideoWithFading() { float fade = 0.25f; while (fade > 0f && Application.isPlaying) { fade -= Time.deltaTime; fade = Mathf.Clamp(fade, 0f, 0.25f); this._display._color = new Color(1f, 1f, 1f, fade / 0.25f); this._display._mediaPlayer.Control.SetVolume(fade / 0.25f); yield return null; } yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame(); if (Application.isPlaying) { if (!this._mediaPlayer.OpenVideoFromFile(this._nextVideoLocation, this._nextVideoPath, this._mediaPlayer.m_AutoStart)) { Debug.LogError("Failed to open video!"); } else { while (Application.isPlaying && (SimpleController.VideoIsReady(this._mediaPlayer) || SimpleController.AudioIsReady(this._mediaPlayer))) { yield return null; } yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame(); } } while (fade < 0.25f && Application.isPlaying) { fade += Time.deltaTime; fade = Mathf.Clamp(fade, 0f, 0.25f); this._display._color = new Color(1f, 1f, 1f, fade / 0.25f); this._display._mediaPlayer.Control.SetVolume(fade / 0.25f); yield return null; } yield break; } private void OnGUI() { if (this._mediaPlayer == null) { return; } GUI.depth = -10; if (this._guiSkin != null) { GUI.skin = this._guiSkin; } GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3((float)Screen.width / 960f, (float)Screen.height / 540f, 1f)); GUILayout.BeginVertical("box", new GUILayoutOption[0]); if (this._mediaPlayer.Control != null) { GUILayout.Label("Loaded: " + this._mediaPlayer.m_VideoPath, new GUILayoutOption[0]); GUILayout.Label(string.Format("Size: {0}x{1} FPS: {3} Duration: {2}ms", new object[] { this._width, this._height, this._mediaPlayer.Info.GetDurationMs(), this._mediaPlayer.Info.GetVideoFrameRate().ToString("F2") }), new GUILayoutOption[0]); GUILayout.Label(string.Concat(new object[] { "Updates: ", this._mediaPlayer.TextureProducer.GetTextureFrameCount(), " Rate: ", this._mediaPlayer.Info.GetVideoDisplayRate().ToString("F1") }), new GUILayoutOption[0]); GUILayout.BeginHorizontal(new GUILayoutOption[0]); this._useFading = GUILayout.Toggle(this._useFading, "Fade to Black During Loading", new GUILayoutOption[0]); this._mediaPlayer.m_AutoStart = GUILayout.Toggle(this._mediaPlayer.m_AutoStart, "Auto Play After Load", new GUILayoutOption[0]); bool loop = this._mediaPlayer.m_Loop; bool flag = GUILayout.Toggle(loop, "Loop", new GUILayoutOption[0]); if (flag != loop) { this._mediaPlayer.m_Loop = flag; this._mediaPlayer.Control.SetLooping(flag); } GUILayout.EndHorizontal(); int num = (int)this._mediaPlayer.Control.GetCurrentTimeMs(); int num2 = (int)GUILayout.HorizontalSlider((float)num, 0f, this._durationSeconds * 1000f, new GUILayoutOption[0]); Rect lastRect = GUILayoutUtility.GetLastRect(); float x = GUI.skin.horizontalSliderThumb.CalcSize(GUIContent.none).x; Rect position = lastRect; GUI.color = Color.green; position.xMin += x; position.y = position.yMax - 4f; position.width -= x * 1f; position.width *= this._mediaPlayer.Control.GetBufferingProgress(); position.height = 4f; GUI.DrawTexture(position, Texture2D.whiteTexture, ScaleMode.StretchToFill); GUI.color = Color.green; int bufferedTimeRangeCount = this._mediaPlayer.Control.GetBufferedTimeRangeCount(); for (int i = 0; i < bufferedTimeRangeCount; i++) { float num3 = 0f; float num4 = 0f; if (this._mediaPlayer.Control.GetBufferedTimeRange(i, ref num3, ref num4)) { position.xMin = x + lastRect.x + (lastRect.width - x * 1f) * (num3 / (this._durationSeconds * 1000f)); position.xMax = x + lastRect.x + (lastRect.width - x * 1f) * (num4 / (this._durationSeconds * 1000f)); GUI.DrawTexture(position, Texture2D.whiteTexture, ScaleMode.StretchToFill); } } GUI.color = Color.white; if (num2 != num) { this._mediaPlayer.Control.Seek((float)num2); } if (!this._mediaPlayer.Control.IsPlaying()) { if (GUILayout.Button("Play", new GUILayoutOption[0])) { this._mediaPlayer.Control.Play(); } } else if (GUILayout.Button("Pause", new GUILayoutOption[0])) { this._mediaPlayer.Control.Pause(); } GUILayout.BeginHorizontal(new GUILayoutOption[0]); int audioTrackCount = this._mediaPlayer.Info.GetAudioTrackCount(); int currentAudioTrack = this._mediaPlayer.Control.GetCurrentAudioTrack(); for (int j = 0; j < audioTrackCount; j++) { if (j == currentAudioTrack) { GUI.color = Color.green; } if (GUILayout.Button("Audio Track #" + (j + 1), new GUILayoutOption[0])) { this._mediaPlayer.Control.SetAudioTrack(j); } GUI.color = Color.white; } GUILayout.EndHorizontal(); } GUILayout.Label("Select a new file to play:", new GUILayoutOption[0]); int num5 = GUILayout.SelectionGrid(-1, this._filenames, 3, new GUILayoutOption[0]); if (num5 >= 0) { this.LoadVideo(Path.Combine(this._folder, this._filenames[num5]), false); } GUILayout.Space(8f); GUILayout.Label("Select a new stream to play:", new GUILayoutOption[0]); int num6 = GUILayout.SelectionGrid(-1, this._streams, 1, new GUILayoutOption[0]); if (num6 >= 0) { this.LoadVideo(this._streams[num6], true); } GUILayout.Space(8f); GUILayout.Label("Recent Events: ", new GUILayoutOption[0]); GUILayout.BeginVertical("box", new GUILayoutOption[0]); int num7 = 0; foreach (string text in this._eventLog) { GUI.color = Color.white; if (num7 == 0) { GUI.color = new Color(1f, 1f, 1f, this._eventTimer); } GUILayout.Label(text, new GUILayoutOption[0]); num7++; } GUILayout.EndVertical(); GUI.color = Color.white; GUILayout.EndVertical(); } public string _folder = "AVProVideoSamples/"; public string[] _filenames = new string[] { "SampleSphere.mp4", "BigBuckBunny_360p30.mp3", "BigBuckBunny_720p30.mp4" }; public string[] _streams; public MediaPlayer _mediaPlayer; public DisplayIMGUI _display; public GUISkin _guiSkin; private int _width; private int _height; private float _durationSeconds; public bool _useFading = true; private Queue _eventLog = new Queue(8); private float _eventTimer = 1f; private MediaPlayer.FileLocation _nextVideoLocation; private string _nextVideoPath; } }