Menu

[r326]: / trunk / framework / OSMF / org / osmf / elements / CompositeElement.as  Maximize  Restore  History

Download this file

406 lines (365 with data), 11.5 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*****************************************************
*
* 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
{
import __AS3__.vec.Vector;
import flash.errors.IllegalOperationError;
import org.osmf.elements.compositeClasses.CompositeMediaTraitFactory;
import org.osmf.elements.compositeClasses.CompositeMetadata;
import org.osmf.elements.compositeClasses.TraitAggregator;
import org.osmf.elements.compositeClasses.TraitAggregatorEvent;
import org.osmf.events.MediaErrorEvent;
import org.osmf.media.MediaElement;
import org.osmf.media.MediaResourceBase;
import org.osmf.metadata.Metadata;
import org.osmf.traits.MediaTraitBase;
import org.osmf.utils.OSMFStrings;
/**
* CompositeElement is a media element which encapsulates a collection
* of other more granular media elements. It is generally referred to as
* a <b>media composition</b>.
*
* <p>The media elements that make up a media composition are treated
* as a single, unified media element. For example, if a media composition
* encapsulates a sequence of videos, the CompositeElement will behave as
* if it's a single VideoElement, but one which plays several videos in
* sequence.</p>
*
* <p>Because a CompositeElement maintains a list of MediaElement children,
* any of which may be CompositeElements themselves, a media composition
* can be expressed as a tree structure.</p>
*
* <p>Typically a CompositeElement is not instantiated directly but instead is used as
* the base class for creating specific types of media compositions.</p>
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class CompositeElement extends MediaElement
{
/**
* Constructor.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function CompositeElement()
{
super();
_traitFactory = new CompositeMediaTraitFactory();
setupTraitAggregator();
}
/**
* The number of child MediaElements in this media composition.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get numChildren():int
{
return children.length;
}
/**
* Gets the child at the specified index.
*
* @param index The index in the list from which to retrieve the child.
*
* @return The child at that index or <code>null</code> if there is none.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getChildAt(index:int):MediaElement
{
if (index >= 0 && index < children.length)
{
return children[index];
}
else
{
return null;
}
}
/**
* Returns the index of the child if it is in the list such that
* <code>getChildAt(index) == child</code>.
*
* @param child The child to find.
*
* @return The index of the child or -1 if the child is not in the
* list.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getChildIndex(child:MediaElement):int
{
return children.indexOf(child);
}
/**
* Adds the specified child to the end of the list. Equivalent to
* <code>addChildAt(child,numChildren)</code>.
*
* @param child The child to add.
*
* @throws ArgumentError If child is <code>null</code>.
* @throws RangeError If the specified index is less than zero or
* greater than the length of the list.
* @throws IllegalOperation If the child is already a child.
*
* @return The MediaElement that you pass in the child parameter.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function addChild(child:MediaElement):MediaElement
{
return addChildAt(child, numChildren);
}
/**
* Adds the child to the list at the specified index. If a child
* already exists at this index, it and all subsequent children
* will have their index positions increased by one.
*
* @param child The child to add.
* @param index The index position at which to add the child.
*
* @throws ArgumentError If child is <code>null</code>.
* @throws RangeError If the specified index is less than zero or
* greater than the length of the list.
* @throws IllegalOperation If the child is already a child.
*
* @return The MediaElement that you pass in the child parameter.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function addChildAt(child:MediaElement,index:Number):MediaElement
{
if (child == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
if (index < 0 || index > numChildren)
{
throw new RangeError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
if (children.indexOf(child) != -1)
{
throw new IllegalOperationError(OSMFStrings.getString(OSMFStrings.ALREADY_ADDED));
}
children.splice(index, 0, child);
processAddedChild(child, index);
return child;
}
/**
* Removes the specified child and returns it. Equivalent to
* <code>removeChildAt(child,getChildIndex(child))</code>.
*
* @param child The child MediaElement to remove.
*
* @return The MediaElement that you pass in the child parameter.
*
* @throws ArgumentError If the child is not a child of this composition.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function removeChild(child:MediaElement):MediaElement
{
var index:int = children.indexOf(child);
if (index == -1)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
return removeChildAt(index);
}
/**
* Removes the child at the specified index and returns it. Any
* children with index positions greater than this index have their index positions decreased by one.
*
* @param index The index from which to remove the child.
*
* @return The child at that index.
*
* @throws RangeError If the specified index is less than zero or
* greater than the length of the list.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function removeChildAt(index:int):MediaElement
{
var child:MediaElement = null;
if (index >= 0 && index < children.length)
{
child = children.splice(index, 1)[0] as MediaElement;
processRemovedChild(child);
}
else
{
throw new RangeError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
return child;
}
// Overrides
//
/**
* @private
*/
override public function set resource(value:MediaResourceBase):void
{
// No-op -- a CompositeElement has no notion of a media resource.
}
// Protected
//
/**
* @private
*
* Invoked when a child of the composite element is added. Subclasses
* can override to do custom processing.
*/
protected function processAddedChild(child:MediaElement, index:int):void
{
child.addEventListener(MediaErrorEvent.MEDIA_ERROR, onChildError);
compositeMetadata.addChild(child.metadata);
if (traitAggregator)
{
// The Trait Aggregator needs to keep track of the traits for the
// new child.
traitAggregator.addChildAt(child, index);
}
}
/**
* @private
*
* Invoked when a child of the composite element is removed. Subclasses
* can override to do custom processing.
*/
protected function processRemovedChild(child:MediaElement):void
{
child.removeEventListener(MediaErrorEvent.MEDIA_ERROR, onChildError);
if (traitAggregator)
{
// The Trait Aggregator no longer needs to keep track of the traits
// for this child, it has been removed.
traitAggregator.removeChild(child);
}
compositeMetadata.removeChild(child.metadata);
}
/**
* @private
*
* Invoked when the specified trait of the specified MediaTraitType is aggregated
* by the TraitAggregator used by this CompositeElement. Subclasses
* can override to do custom processing.
*/
protected function processAggregatedTrait(traitType:String, trait:MediaTraitBase):void
{
}
/**
* @private
*
* Invoked when the specified trait of the specified MediaTraitType is
* unaggregated by the TraitAggregator used by this CompositeElement.
* Subclasses can override to do custom processing.
*/
protected function processUnaggregatedTrait(traitType:String, trait:MediaTraitBase):void
{
}
/**
* @private
*/
override protected function createMetadata():Metadata
{
return new CompositeMetadata();
}
/**
* @private
*/
protected function get compositeMetadata():CompositeMetadata
{
return CompositeMetadata(metadata);
}
/**
* @private
*
* The TraitAggregator for this CompositeElement. Used to aggregate
* traits across the children of the composition. Subclasses can set
* this via <code>createTraitAggregator()</code>.
*/
protected final function get traitAggregator():TraitAggregator
{
return _traitAggregator;
}
/**
* @private
*
* Factory class used to create composite traits.
*/
protected final function get traitFactory():CompositeMediaTraitFactory
{
return _traitFactory;
}
// Internals
//
private function setupTraitAggregator():void
{
_traitAggregator = new TraitAggregator();
traitAggregator.addEventListener(TraitAggregatorEvent.TRAIT_AGGREGATED, onTraitAggregated);
traitAggregator.addEventListener(TraitAggregatorEvent.TRAIT_UNAGGREGATED, onTraitUnaggregated);
}
private function onTraitAggregated(event:TraitAggregatorEvent):void
{
processAggregatedTrait(event.traitType, event.trait);
}
private function onTraitUnaggregated(event:TraitAggregatorEvent):void
{
processUnaggregatedTrait(event.traitType, event.trait);
}
private function onChildError(event:MediaErrorEvent):void
{
dispatchEvent(event.clone());
}
private var _traitFactory:CompositeMediaTraitFactory;
private var children:Vector.<MediaElement> = new Vector.<MediaElement>();
private var _traitAggregator:TraitAggregator;
}
}
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.