BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / SampleFramework / Usage / Stereo180Video / Scripts / MoviePlayerSample.cs

MoviePlayerSample.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
MoviePlayerSample.cs 6.8 KiB Download Raw
1/************************************************************************************
2
3Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
4
5See SampleFramework license.txt for license terms. Unless required by applicable law
6or agreed to in writing, the sample code is provided “AS IS” WITHOUT WARRANTIES OR
7CONDITIONS OF ANY KIND, either express or implied. See the license for specific
8language governing permissions and limitations under the license.
9
10************************************************************************************/
11
12using UnityEngine;
13using System;
14using System.IO;
15
16public class MoviePlayerSample : MonoBehaviour
17{
18 private bool videoPausedBeforeAppPause = false;
19
20 private UnityEngine.Video.VideoPlayer videoPlayer = null;
21 private OVROverlay overlay = null;
22 private Renderer mediaRenderer = null;
23
24 public bool isPlaying { get; private set; }
25
26 private RenderTexture copyTexture;
27 private Material externalTex2DMaterial;
28
29 public string MovieName;
30
31 /// <summary>
32 /// Initialization of the movie surface
33 /// </summary>
34 void Awake()
35 {
36 Debug.Log("MovieSample Awake");
37
38 mediaRenderer = GetComponent<Renderer>();
39
40 videoPlayer = GetComponent<UnityEngine.Video.VideoPlayer>();
41 if (videoPlayer == null)
42 videoPlayer = gameObject.AddComponent<UnityEngine.Video.VideoPlayer>();
43
44 overlay = GetComponent<OVROverlay>();
45 if (overlay == null)
46 overlay = gameObject.AddComponent<OVROverlay>();
47
48 // set shape to Equirect
49 overlay.currentOverlayShape = OVROverlay.OverlayShape.Equirect;
50
51 // set source and dest matrices for 180 video
52 overlay.overrideTextureRectMatrix = true;
53 overlay.SetSrcDestRects(new Rect(0, 0, 0.5f, 1.0f), new Rect(0.5f, 0, 0.5f, 1.0f), new Rect(0.25f, 0, 0.5f, 1.0f), new Rect(0.25f, 0, 0.5f, 1.0f));
54
55 // disable it to reset it.
56 overlay.enabled = false;
57 // only can use external surface with native plugin
58 overlay.isExternalSurface = NativeVideoPlayer.IsAvailable;
59 // only mobile has Equirect shape
60 overlay.enabled = Application.platform == RuntimePlatform.Android;
61
62#if UNITY_EDITOR
63 overlay.currentOverlayShape = OVROverlay.OverlayShape.Quad;
64 overlay.enabled = true;
65#endif
66 }
67
68 private System.Collections.IEnumerator Start()
69 {
70 if (mediaRenderer.material == null)
71 {
72 Debug.LogError("No material for movie surface");
73 yield break;
74 }
75
76 // wait 1 second to start (there is a bug in Unity where starting
77 // the video too soon will cause it to fail to load)
78 yield return new WaitForSeconds(1.0f);
79
80 if (!string.IsNullOrEmpty(MovieName))
81 {
82#if UNITY_EDITOR
83 // in editor, just pull in the movie file from wherever it lives (to test without putting in streaming assets)
84 var guids = UnityEditor.AssetDatabase.FindAssets(Path.GetFileNameWithoutExtension(MovieName));
85
86 if (guids.Length > 0)
87 {
88 string video = UnityEditor.AssetDatabase.GUIDToAssetPath(guids[0]);
89 Play(video);
90 }
91#else
92 Play(Application.streamingAssetsPath +"/" + MovieName);
93#endif
94 }
95 }
96
97 public void Play(string moviePath)
98 {
99 if (moviePath != string.Empty)
100 {
101 Debug.Log("Playing Video: " + moviePath);
102 if (overlay.isExternalSurface)
103 {
104 OVROverlay.ExternalSurfaceObjectCreated surfaceCreatedCallback = () =>
105 {
106 Debug.Log("Playing ExoPlayer with SurfaceObject");
107 NativeVideoPlayer.PlayVideo(moviePath, overlay.externalSurfaceObject);
108 };
109
110 if (overlay.externalSurfaceObject == IntPtr.Zero)
111 {
112 overlay.externalSurfaceObjectCreated = surfaceCreatedCallback;
113 }
114 else
115 {
116 surfaceCreatedCallback.Invoke();
117 }
118 }
119 else
120 {
121 Debug.Log("Playing Unity VideoPlayer");
122 videoPlayer.url = moviePath;
123 videoPlayer.Prepare();
124 videoPlayer.Play();
125 }
126
127 Debug.Log("MovieSample Start");
128 isPlaying = true;
129 }
130 else
131 {
132 Debug.LogError("No media file name provided");
133 }
134 }
135
136 public void Play()
137 {
138 if (overlay.isExternalSurface)
139 {
140 NativeVideoPlayer.Play();
141 }
142 else
143 {
144 videoPlayer.Play();
145 }
146 isPlaying = true;
147 }
148
149 public void Pause()
150 {
151 if (overlay.isExternalSurface)
152 {
153 NativeVideoPlayer.Pause();
154 }
155 else
156 {
157 videoPlayer.Pause();
158 }
159 isPlaying = false;
160 }
161
162 void Update()
163 {
164 if (!overlay.isExternalSurface)
165 {
166 var displayTexture = videoPlayer.texture != null ? videoPlayer.texture : Texture2D.blackTexture;
167 if (overlay.enabled)
168 {
169 if (overlay.textures[0] != displayTexture)
170 {
171 // OVROverlay won't check if the texture changed, so disable to clear old texture
172 overlay.enabled = false;
173 overlay.textures[0] = displayTexture;
174 overlay.enabled = true;
175 }
176 }
177 else
178 {
179 mediaRenderer.material.mainTexture = displayTexture;
180 mediaRenderer.material.SetVector("_SrcRectLeft", overlay.srcRectLeft.ToVector());
181 mediaRenderer.material.SetVector("_SrcRectRight", overlay.srcRectRight.ToVector());
182 }
183 }
184 }
185
186 public void Rewind()
187 {
188 if (overlay.isExternalSurface)
189 {
190 NativeVideoPlayer.SetPlaybackSpeed(-1);
191 }
192 else
193 {
194 videoPlayer.playbackSpeed = -1;
195 }
196 }
197
198 public void Stop()
199 {
200 if (overlay.isExternalSurface)
201 {
202 NativeVideoPlayer.Stop();
203 }
204 else
205 {
206 videoPlayer.Stop();
207 }
208
209 isPlaying = false;
210 }
211
212 /// <summary>
213 /// Pauses video playback when the app loses or gains focus
214 /// </summary>
215 void OnApplicationPause(bool appWasPaused)
216 {
217 Debug.Log("OnApplicationPause: " + appWasPaused);
218 if (appWasPaused)
219 {
220 videoPausedBeforeAppPause = !isPlaying;
221 }
222
223 // Pause/unpause the video only if it had been playing prior to app pause
224 if (!videoPausedBeforeAppPause)
225 {
226 if (appWasPaused)
227 {
228 Pause();
229 }
230 else
231 {
232 Play();
233 }
234 }
235 }
236}