Menu

[r13]: / trunk / libs / OSMF / org / osmf / elements / compositeClasses / CompositeTimeTrait.as  Maximize  Restore  History

Download this file

362 lines (326 with data), 10.8 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
package org.osmf.elements.compositeClasses
{
import org.osmf.elements.SerialElement;
import org.osmf.events.TimeEvent;
import org.osmf.media.MediaElement;
import org.osmf.traits.MediaTraitBase;
import org.osmf.traits.MediaTraitType;
import org.osmf.traits.PlayTrait;
import org.osmf.traits.TimeTrait;
/**
* Implementation of TimeTrait which can be a composite media trait.
*
* For parallel media elements, the composite trait represents a timeline
* that encapsulates the timeline of all children. Its duration is the
* maximum of the durations of all children. Its currentTime is kept in sync
* for all children (with the obvious caveat that a child's currentTime will
* never be greater than its duration).
*
* For serial elements, the composite trait represents a timeline that
* encapsulates the timeline of all children. Its duration is the sum of
* the durations of all children. Its currentTime is the sum of the currentTimes
* of the first N fully complete children, plus the currentTime of the next
* child.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
internal class CompositeTimeTrait extends TimeTrait implements IReusable
{
/**
* Constructor.
*
* @param traitAggregator The object which is aggregating all instances
* of the TimeTrait within this composite trait.
* @param mode The composition mode to which this composite trait
* should adhere. See CompositionMode for valid values.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function CompositeTimeTrait(traitAggregator:TraitAggregator, mode:String, owner:MediaElement)
{
super();
this.mode = mode;
this.traitAggregator = traitAggregator;
this.owner = owner;
traitAggregationHelper = new TraitAggregationHelper
( traitType
, traitAggregator
, processAggregatedChild
, processUnaggregatedChild
);
}
/**
* @private
*/
public function attach():void
{
traitAggregationHelper.attach();
}
/**
* @private
*/
public function detach():void
{
traitAggregationHelper.detach();
}
override public function dispose():void
{
}
/**
* @private
*/
override public function get currentTime():Number
{
updateCurrentTime();
return super.currentTime;
}
override protected function signalComplete():void
{
// The base class can cause this method to get called, sometimes
// inappropriately for serial elements, so we need to verify that
// it's truly complete.
if ( mode == CompositionMode.PARALLEL
|| isSerialComplete()
)
{
super.signalComplete()
}
}
private function isSerialComplete():Boolean
{
// A serial element is only complete if it's truly at the end.
//
var childTimeTrait:TimeTrait = traitAggregator.listenedChild.getTrait(MediaTraitType.TIME) as TimeTrait;
// Conditions in which it's not at the end:
// 1) If currentTime == duration because we don't yet have the duration
// for the next child.
// 2) If the next child doesn't have a duration yet (FM-303).
var result:Boolean =
traitAggregator.getChildIndex(traitAggregator.listenedChild) == traitAggregator.numChildren - 1
&& childTimeTrait.duration > 0
&& !isNaN(childTimeTrait.duration);
// 3) If the last child is itself a SerialElement, then we need to
// recursively apply the same rule checks, in case it appears to be
// complete but really isn't (FM-707).
if ( result
&& childTimeTrait is CompositeTimeTrait
&& traitAggregator.listenedChild is SerialElement
)
{
result = CompositeTimeTrait(childTimeTrait).isSerialComplete();
}
return result;
}
// Internal
//
/**
* @private
*/
private function processAggregatedChild(child:MediaTraitBase):void
{
child.addEventListener(TimeEvent.DURATION_CHANGE, onDurationChanged, false, 0, true);
child.addEventListener(TimeEvent.COMPLETE, onComplete, false, 0, true);
updateDuration();
updateCurrentTime();
}
/**
* @private
*/
private function processUnaggregatedChild(child:MediaTraitBase):void
{
child.removeEventListener(TimeEvent.DURATION_CHANGE, onDurationChanged);
child.removeEventListener(TimeEvent.COMPLETE, onComplete);
updateDuration();
updateCurrentTime();
}
private function onDurationChanged(event:TimeEvent):void
{
updateDuration();
}
private function onComplete(event:TimeEvent):void
{
var timeTrait:TimeTrait = event.target as TimeTrait;
if (mode == CompositionMode.PARALLEL)
{
// If every child has reached their duration, then we should
// dispatch the complete event.
var allHaveReachedDuration:Boolean = true;
traitAggregator.forEachChildTrait
(
function(mediaTrait:MediaTraitBase):void
{
var iterTimeTrait:TimeTrait = TimeTrait(mediaTrait);
// Assume that the child that fired the event has
// finished.
if (iterTimeTrait != timeTrait &&
iterTimeTrait.currentTime < iterTimeTrait.duration)
{
allHaveReachedDuration = false;
}
}
, MediaTraitType.TIME
);
if (allHaveReachedDuration)
{
dispatchEvent(new TimeEvent(TimeEvent.COMPLETE));
}
}
else // SERIAL
{
if (timeTrait == traitOfCurrentChild)
{
// If the composite element has the PlayTrait and the
// current child has another sibling ahead of it, then
// the next sibling with a PlayTrait should be played.
var playTrait:PlayTrait = owner.getTrait(MediaTraitType.PLAY) as PlayTrait;
if (playTrait != null)
{
// Note that we don't check whether to dispatch the
// complete event until we determine that
// there's no more playable children -- otherwise we'd
// almost certainly dispatch it when it shouldn't be
// dispatched since subsequent children are likely to
// lack the temporal trait until they're loaded.
SerialElementTransitionManager.playNextPlayableChild
( traitAggregator
, checkDispatchCompleteEvent
);
}
else
{
checkDispatchCompleteEvent();
}
}
}
}
private function checkDispatchCompleteEvent():void
{
// If the current child is the last temporal child, then we should
// dispatch the complete event.
var nextChild:MediaElement =
traitAggregator.getNextChildWithTrait
( traitAggregator.listenedChild
, MediaTraitType.TIME
);
if (nextChild == null)
{
super.signalComplete();
}
}
private function updateDuration():void
{
var newDuration:Number = 0;
var hasChildWithDuration:Boolean = false;
traitAggregator.forEachChildTrait
(
function(mediaTrait:MediaTraitBase):void
{
var childDuration:Number = TimeTrait(mediaTrait).duration;
if (!isNaN(childDuration))
{
hasChildWithDuration = true;
if (mode == CompositionMode.PARALLEL)
{
// The duration is the max of all child durations.
newDuration = Math.max(newDuration, childDuration);
}
else // SERIAL
{
// The duration is the sum of all child durations.
newDuration += childDuration;
}
}
}
, MediaTraitType.TIME
);
setDuration(hasChildWithDuration ? newDuration : NaN);
}
private function updateCurrentTime():void
{
var newCurrentTime:Number = 0;
var hasChildWithCurrentTime:Boolean = false;
var serialCurrentTimeCalculated:Boolean = false;
traitAggregator.forEachChildTrait
(
function(mediaTrait:MediaTraitBase):void
{
var childCurrentTime:Number = TimeTrait(mediaTrait).currentTime;
if (isNaN(childCurrentTime))
{
childCurrentTime = 0;
}
else
{
hasChildWithCurrentTime = true;
}
if (mode == CompositionMode.PARALLEL)
{
// The currentTime is the max of all child currentTimes.
newCurrentTime = Math.max(newCurrentTime, childCurrentTime);
}
else // SERIAL
{
// The currentTime is the sum of all durations up to the
// current child, plus the currentTime of the current
// child.
if (!serialCurrentTimeCalculated)
{
if (mediaTrait == traitOfCurrentChild)
{
newCurrentTime += childCurrentTime;
serialCurrentTimeCalculated = true;
}
else
{
var duration:Number = TimeTrait(mediaTrait).duration;
if (!isNaN(duration))
{
newCurrentTime += duration;
}
}
}
}
}
, MediaTraitType.TIME
);
setCurrentTime(hasChildWithCurrentTime ? newCurrentTime : NaN);
}
private function get traitOfCurrentChild():TimeTrait
{
return traitAggregator.listenedChild
? traitAggregator.listenedChild.getTrait(MediaTraitType.TIME) as TimeTrait
: null;
}
private var traitAggregator:TraitAggregator;
private var traitAggregationHelper:TraitAggregationHelper;
private var mode:String;
private var owner:MediaElement;
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.