AVProWindowsMediaMovie.cs
8.13 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
//-----------------------------------------------------------------------------
// Copyright 2012-2016 RenderHeads Ltd. All rights reserverd.
//-----------------------------------------------------------------------------
[System.Serializable]
public class AVProWindowsMediaMovieClip
{
public string name;
public int inPoint = -1;
public int outPoint = -1;
public AVProWindowsMediaMovieClip(string name, int inPoint, int outPoint)
{
this.name = name;
this.inPoint = inPoint;
this.outPoint = outPoint;
}
}
[AddComponentMenu("AVPro Windows Media/Movie")]
[ExecuteInEditMode]
public class AVProWindowsMediaMovie : MonoBehaviour
{
protected AVProWindowsMedia _moviePlayer;
public string _folder = "./";
public string _filename = "movie.avi";
public bool _useStreamingAssetsPath;
public bool _loop = false;
public ColourFormat _colourFormat = ColourFormat.YCbCr_HD;
public bool _allowAudio = true;
public bool _useAudioDelay = false;
public bool _useAudioMixer = false;
public bool _useDisplaySync = false;
public bool _loadOnStart = true;
public bool _playOnStart = true;
public bool _editorPreview = false;
public bool _ignoreFlips = true;
public float _volume = 1.0f;
public float _audioBalance = 0.0f;
public FilterMode _textureFilterMode = FilterMode.Bilinear;
public TextureWrapMode _textureWrapMode = TextureWrapMode.Clamp;
[SerializeField]
private List<AVProWindowsMediaMovieClip> _clips;
private Dictionary<string, AVProWindowsMediaMovieClip> _clipLookup = new Dictionary<string, AVProWindowsMediaMovieClip>();
private AVProWindowsMediaMovieClip _currentClip;
public enum ColourFormat
{
RGBA32,
YCbCr_SD,
YCbCr_HD,
}
public Texture OutputTexture
{
get { if (_moviePlayer != null) return _moviePlayer.OutputTexture; return null; }
}
public AVProWindowsMedia MovieInstance
{
get { return _moviePlayer; }
}
public virtual void Start()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
return;
#endif
if (null == AVProWindowsMediaManager.Instance)
{
throw new System.Exception("You need to add AVProWindowsMediaManager component to your scene.");
}
if (_loadOnStart)
{
LoadMovie(_playOnStart);
}
}
public virtual bool LoadMovie(bool autoPlay)
{
bool result = true;
if (_moviePlayer == null)
_moviePlayer = new AVProWindowsMedia();
LoadClips();
bool allowNativeFormat = (_colourFormat != ColourFormat.RGBA32);
string filePath = GetFilePath();
if (_moviePlayer.StartVideo(filePath, allowNativeFormat, _colourFormat == ColourFormat.YCbCr_HD, _allowAudio, _useAudioDelay, _useAudioMixer, _useDisplaySync, _ignoreFlips, _textureFilterMode, _textureWrapMode))
{
if (_allowAudio)
{
_moviePlayer.Volume = _volume;
_moviePlayer.AudioBalance = _audioBalance;
}
_moviePlayer.Loop = _loop;
if (autoPlay)
{
_moviePlayer.Play();
}
}
else
{
Debug.LogWarning("[AVProWindowsMedia] Couldn't load movie " + _filename);
UnloadMovie();
result = false;
}
return result;
}
public bool LoadMovieFromMemory(bool autoPlay, string name, System.IntPtr moviePointer, long movieLength, FilterMode textureFilterMode, TextureWrapMode textureWrapMode)
{
bool result = true;
if (_moviePlayer == null)
_moviePlayer = new AVProWindowsMedia();
bool allowNativeFormat = (_colourFormat != ColourFormat.RGBA32);
if (_moviePlayer.StartVideoFromMemory(name, moviePointer, movieLength, allowNativeFormat, _colourFormat == ColourFormat.YCbCr_HD, _allowAudio, _useAudioDelay, _useAudioMixer, _useDisplaySync, _ignoreFlips, textureFilterMode, textureWrapMode))
{
if (_allowAudio)
{
_moviePlayer.Volume = _volume;
_moviePlayer.AudioBalance = _audioBalance;
}
_moviePlayer.Loop = _loop;
if (autoPlay)
{
_moviePlayer.Play();
}
}
else
{
Debug.LogWarning("[AVProWindowsMedia] Couldn't load movie " + _filename);
UnloadMovie();
result = false;
}
return result;
}
public void Update()
{
if (_moviePlayer != null)
{
_volume = Mathf.Clamp01(_volume);
if (_allowAudio)
{
if (_volume != _moviePlayer.Volume)
_moviePlayer.Volume = _volume;
if (_audioBalance != _moviePlayer.AudioBalance)
_moviePlayer.AudioBalance = _audioBalance;
}
if (_loop != _moviePlayer.Loop)
_moviePlayer.Loop = _loop;
_moviePlayer.Update(false);
// When the movie finishes playing, send a message so it can be handled
if (!_moviePlayer.Loop && _moviePlayer.IsPlaying && _moviePlayer.IsFinishedPlaying)
{
_moviePlayer.Pause();
this.SendMessage("MovieFinished", this, SendMessageOptions.DontRequireReceiver);
}
}
}
public void Play()
{
if (_moviePlayer != null)
_moviePlayer.Play();
}
public void Pause()
{
if (_moviePlayer != null)
_moviePlayer.Pause();
}
public int NumClips
{
get { if (_clips != null) return _clips.Count; return 0; }
}
public string GetClipName(int index)
{
string result = string.Empty;
if (_clips != null && index >= 0 && index < _clips.Count)
result = _clips[index].name;
return result;
}
public void ClearClips()
{
_currentClip = null;
_clips.Clear();
_clipLookup.Clear();
}
public void AddClip(string name, int inPoint, int outPoint)
{
AVProWindowsMediaMovieClip clip = new AVProWindowsMediaMovieClip(name, inPoint, outPoint);
_clips.Add(clip);
_clipLookup.Add(name, clip);
}
public string GetCurrentClipName()
{
string result = string.Empty;
if (_currentClip != null)
result = _currentClip.name;
return result;
}
public void LoadClips()
{
_clipLookup.Clear();
if (_clips != null && _clips.Count > 0)
{
for (int i = 0; i < _clips.Count; i++)
{
if (!string.IsNullOrEmpty(_clips[i].name))
{
_clipLookup.Add(_clips[i].name, _clips[i]);
}
}
}
}
public void ResetClip()
{
_currentClip = null;
MovieInstance.SetFrameRange(-1, -1);
}
public void PlayClip(string name, bool loop, bool startPaused)
{
if (MovieInstance == null)
throw new System.Exception("Movie instance is null");
if (!_clipLookup.ContainsKey(name))
throw new System.Exception("Frame range key not found");
MovieInstance.Loop = loop;
_currentClip = _clipLookup[name];
MovieInstance.SetFrameRange(_currentClip.inPoint, _currentClip.outPoint);
MovieInstance.PositionFrames = (uint)_currentClip.inPoint;
if (!startPaused)
MovieInstance.Play();
else
MovieInstance.Pause();
}
public virtual void UnloadMovie()
{
if (_moviePlayer != null)
{
_moviePlayer.Dispose();
_moviePlayer = null;
}
}
public void OnDestroy()
{
UnloadMovie();
}
public string GetFilePath()
{
string filePath = Path.Combine(_folder, _filename);
if (_useStreamingAssetsPath)
{
filePath = Path.Combine(Application.streamingAssetsPath, filePath);
}
// 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.
else if (!Application.isEditor && !Path.IsPathRooted(filePath))
{
string rootPath = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
filePath = Path.Combine(rootPath, filePath);
}
return filePath;
}
#if UNITY_EDITOR && !UNITY_WEBPLAYER
[ContextMenu("Save PNG")]
private void SavePNG()
{
if (OutputTexture != null && _moviePlayer != null)
{
Texture2D tex = new Texture2D(OutputTexture.width, OutputTexture.height, TextureFormat.ARGB32, false);
RenderTexture.active = (RenderTexture)OutputTexture;
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0, false);
tex.Apply(false, false);
byte[] pngBytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes("AVProWindowsMedia-image" + Random.Range(0, 65536).ToString("X") + ".png", pngBytes);
RenderTexture.active = null;
Texture2D.Destroy(tex);
tex = null;
}
}
#endif
}