123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- 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<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(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<string> _eventLog = new Queue<string>(8);
- private float _eventTimer = 1f;
- private MediaPlayer.FileLocation _nextVideoLocation;
- private string _nextVideoPath;
- }
- }
|