AVProWindowsMediaPlayVideoDemo.cs
7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
public class AVProWindowsMediaPlayVideoDemo : MonoBehaviour
{
public GUISkin _skin;
public AVProWindowsMediaMovie _movie;
public AVProWindowsMediaGUIDisplay _display;
private bool _visible = true;
private float _alpha = 1.0f;
private bool _playFromMemory = false;
private GCHandle _bytesHandle;
private System.IntPtr _moviePtr;
private uint _movieLength;
private void ReleaseMemoryFile()
{
if (_bytesHandle.IsAllocated)
_bytesHandle.Free();
_moviePtr = System.IntPtr.Zero;
_movieLength = 0;
}
private void LoadFileToMemory(string folder, string filename)
{
#if !UNITY_WEBPLAYER
string filePath = Path.Combine(folder, filename);
// If we're running outside of the editor we may need to resolve the relative path
// as the working-directory may not be that of the application EXE.
if (!Application.isEditor && !Path.IsPathRooted(filePath))
{
string rootPath = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
filePath = Path.Combine(rootPath, filePath);
}
ReleaseMemoryFile();
if (File.Exists(filePath))
{
byte[] bytes = System.IO.File.ReadAllBytes(filePath);
if (bytes.Length > 0)
{
_bytesHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
_moviePtr = _bytesHandle.AddrOfPinnedObject();
_movieLength = (uint)bytes.Length;
_movie.LoadMovieFromMemory(true, filename, _moviePtr, _movieLength, FilterMode.Bilinear, TextureWrapMode.Clamp);
}
}
#else
Debug.LogError("[AVProWindowsMedia] Loading from memory not supported on this platform. Change platform to Standalone.");
#endif
}
public void OnGUI()
{
GUI.skin = _skin;
if (_visible)
{
GUI.color = new Color(1f, 1f, 1f, _alpha);
GUILayout.BeginArea(new Rect(0, 0, 740, 350), GUI.skin.box);
ControlWindow(0);
GUILayout.EndArea();
}
GUI.color = new Color(1f, 1f, 1f, 1f - _alpha);
GUI.Box(new Rect(0, 0, 128, 32), "Demo Controls");
}
void Update()
{
Rect r = new Rect(0, 0, 740, 350);
if (r.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))
{
_visible = true;
_alpha = 1.0f;
}
else
{
_alpha -= Time.deltaTime * 4f;
if (_alpha <= 0.0f)
{
_alpha = 0.0f;
_visible = false;
}
}
}
public void ControlWindow(int id)
{
if (_movie == null)
return;
GUILayout.Space(16f);
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("Folder: ", GUILayout.Width(100));
_movie._folder = GUILayout.TextField(_movie._folder, 192);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("File: ", GUILayout.Width(100));
_movie._filename = GUILayout.TextField(_movie._filename, 128, GUILayout.Width(440));
if (GUILayout.Button("Load File", GUILayout.Width(90)))
{
if (!_playFromMemory)
{
_movie.LoadMovie(true);
}
else
{
LoadFileToMemory(_movie._folder, _movie._filename);
}
}
GUILayout.EndHorizontal();
if (_display != null)
{
GUILayout.BeginHorizontal();
GUILayout.Space(100f);
/*if (_display._alphaBlend)
_display._alphaBlend = GUILayout.Toggle(_display._alphaBlend, "Rendering with Transparency");
else
_display._alphaBlend = GUILayout.Toggle(_display._alphaBlend, "Rendering without Transparency");*/
if (_display._alphaBlend != GUILayout.Toggle(_display._alphaBlend, "Render with Transparency"))
{
_display._alphaBlend = !_display._alphaBlend;
if (_display._alphaBlend)
{
_movie._colourFormat = AVProWindowsMediaMovie.ColourFormat.RGBA32;
}
else
{
_movie._colourFormat = AVProWindowsMediaMovie.ColourFormat.YCbCr_HD;
}
if (!_playFromMemory)
{
_movie.LoadMovie(true);
}
else
{
LoadFileToMemory(_movie._folder, _movie._filename);
}
}
if (_playFromMemory != GUILayout.Toggle(_playFromMemory, "Play from Memory"))
{
_playFromMemory = !_playFromMemory;
if (_movie.MovieInstance != null)
{
if (!_playFromMemory)
{
_movie.LoadMovie(true);
}
else
{
LoadFileToMemory(_movie._folder, _movie._filename);
}
}
}
GUILayout.EndHorizontal();
}
AVProWindowsMedia moviePlayer = _movie.MovieInstance;
if (moviePlayer != null)
{
GUILayout.BeginHorizontal();
GUILayout.Label("Info:", GUILayout.Width(100f));
GUILayout.Label(moviePlayer.Width + "x" + moviePlayer.Height + " @ " + moviePlayer.FrameRate.ToString("F2") + " FPS");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Volume ", GUILayout.Width(100));
float volume = _movie._volume;
float newVolume = GUILayout.HorizontalSlider(volume, 0.0f, 1.0f, GUILayout.Width(200));
if (volume != newVolume)
{
_movie._volume = newVolume;
}
GUILayout.Label(_movie._volume.ToString("F1"));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Balance ", GUILayout.Width(100));
float balance = _movie._audioBalance;
float newBalance = GUILayout.HorizontalSlider(balance, -1.0f, 1.0f, GUILayout.Width(200));
if (balance != newBalance)
{
_movie._audioBalance = newBalance;
}
GUILayout.Label(moviePlayer.AudioBalance.ToString("F1"));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Audio Delay", GUILayout.Width(100));
int delay = moviePlayer.AudioDelay;
int newDelay = Mathf.FloorToInt(GUILayout.HorizontalSlider(delay, -1000.0f, 1000.0f, GUILayout.Width(200)));
if (delay != newDelay)
{
moviePlayer.AudioDelay = newDelay;
}
float msPerFrame = 1000.0f / moviePlayer.FrameRate;
int frameDelay = Mathf.FloorToInt((float)newDelay / msPerFrame);
GUILayout.Label(moviePlayer.AudioDelay.ToString() + "ms (" + frameDelay + " frames)");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Time ", GUILayout.Width(100));
float position = moviePlayer.PositionSeconds;
float newPosition = GUILayout.HorizontalSlider(position, 0.0f, moviePlayer.DurationSeconds, GUILayout.Width(200));
if (position != newPosition)
{
moviePlayer.PositionSeconds = newPosition;
}
GUILayout.Label(moviePlayer.PositionSeconds.ToString("F2") + " / " + moviePlayer.DurationSeconds.ToString("F3") + "s");
if (GUILayout.Button("Play"))
{
moviePlayer.Play();
}
if (GUILayout.Button("Pause"))
{
moviePlayer.Pause();
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Frame", GUILayout.Width(100f));
uint positionFrame = moviePlayer.PositionFrames;
if (positionFrame != uint.MaxValue)
{
uint newPositionFrame = (uint)GUILayout.HorizontalSlider(positionFrame, 0.0f, (float)moviePlayer.LastFrame, GUILayout.Width(200));
if (positionFrame != newPositionFrame)
{
moviePlayer.PositionFrames = newPositionFrame;
}
GUILayout.Label(moviePlayer.PositionFrames.ToString() + " / " + moviePlayer.LastFrame.ToString());
if (GUILayout.RepeatButton("<", GUILayout.Width(50)))
{
if (moviePlayer.PositionFrames > 0)
{
moviePlayer.PositionFrames--;
}
}
if (GUILayout.RepeatButton(">", GUILayout.Width(50)))
{
if (moviePlayer.PositionFrames < moviePlayer.LastFrame)
{
moviePlayer.PositionFrames++;
}
}
GUILayout.EndHorizontal();
}
GUILayout.BeginHorizontal();
GUILayout.Label("Rate ", GUILayout.Width(100f));
GUILayout.Label(moviePlayer.PlaybackRate.ToString("F2") + "x");
if (GUILayout.Button("-", GUILayout.Width(50)))
{
moviePlayer.PlaybackRate = moviePlayer.PlaybackRate * 0.5f;
}
if (GUILayout.Button("+", GUILayout.Width(50)))
{
moviePlayer.PlaybackRate = moviePlayer.PlaybackRate * 2.0f;
}
GUILayout.EndHorizontal();
#if UNITY_EDITOR
GUILayout.Label("Displaying at " + moviePlayer.DisplayFPS.ToString("F1") + " fps");
#endif
}
GUILayout.EndVertical();
}
}