Menu

[r329]: / trunk / framework / OSMF / org / osmf / media / MediaElement.as  Maximize  Restore  History

Download this file

732 lines (663 with data), 20.4 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*****************************************************
*
* 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.media
{
import __AS3__.vec.Vector;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.Dictionary;
import org.osmf.containers.IMediaContainer;
import org.osmf.events.ContainerChangeEvent;
import org.osmf.events.MediaElementEvent;
import org.osmf.events.MediaErrorEvent;
import org.osmf.events.MetadataEvent;
import org.osmf.metadata.Metadata;
import org.osmf.traits.MediaTraitBase;
import org.osmf.utils.OSMFStrings;
/**
* Dispatched when a trait is added to the media element.
*
* @eventType org.osmf.events.MediaElementEvent.TRAIT_ADD
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="traitAdd",type="org.osmf.events.MediaElementEvent")]
/**
* Dispatched when a trait is removed from the media element.
*
* @eventType org.osmf.events.MediaElementEvent.TRAIT_REMOVE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="traitRemove",type="org.osmf.events.MediaElementEvent")]
/**
* Dispatched when a Metadata object has been added to this media element.
*
* @eventType org.osmf.events.MediaElementEvent.METADATA_ADD
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="metadataAdd",type="org.osmf.events.MediaElementEvent")]
/**
* Dispatched when a Metadata object has been removed from this media element.
*
* @eventType org.osmf.events.MediaElementEvent.METADATA_REMOVE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="metadataRemove",type="org.osmf.events.MediaElementEvent")]
/**
* Dispatched when an error which impacts the operation of the media
* element occurs.
*
* @eventType org.osmf.events.MediaErrorEvent.MEDIA_ERROR
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="mediaError",type="org.osmf.events.MediaErrorEvent")]
/**
* Dispatched when the element's container property changed.
*
* @eventType org.osmf.events.ContainerChangeEvent.CONTAINER_CHANGE
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
[Event(name="containerChange",type="org.osmf.events.ContainerChangeEvent")]
/**
* A MediaElement represents a unified media experience.
* It may consist of a simple media item, such as a video or a sound.
* Different instances (or subclasses) can represent different types of media.
* A MediaElement may also represent a complex media experience composed of
* multiple items, such as videos, ad banners, SWF overlays, background page branding, etc.
*
* <p>Programmatically, a media element encapsulates a set of media traits and
* a state space.
* The media traits
* represent the capabilities of the media element and are dynamic in
* nature. At one moment in time a media element might be
* seekable, at another moment it might not be. For example, this could occur
* if the media element is a video sequence containing unskippable ads.</p>
* <p>A media element operates on a media resource. For example, if the
* media element represents a video player, the media resource might
* encapsulate a URL to a video stream.
* If the media element represents a complex media composition,
* the media resource URL might be a document that references
* the multiple resources used in the media composition.</p>
* @see MediaTraitBase
* @see MediaResourceBase
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public class MediaElement extends EventDispatcher
{
// Public interface
//
/**
* Constructor.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function MediaElement()
{
super();
_metadata = createMetadata();
_metadata.addEventListener(MetadataEvent.VALUE_ADD, onMetadataValueAdd);
_metadata.addEventListener(MetadataEvent.VALUE_REMOVE, onMetadataValueRemove);
_metadata.addEventListener(MetadataEvent.VALUE_CHANGE, onMetadataValueChange);
setupTraitResolvers();
setupTraits();
addEventListener
( ContainerChangeEvent.CONTAINER_CHANGE
, onContainerChange
, false
, Number.MAX_VALUE
);
}
/**
* A Vector of MediaTraitType values representing the trait types on this
* media element.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get traitTypes():Vector.<String>
{
// Return a copy of our types vector.
return _traitTypes.concat();
}
/**
* Determines whether this media element has a media trait of the
* specified type.
*
* @param type The MediaTraitType for the media trait to check for.
*
* @throws ArgumentError If the parameter is <code>null</code>.
* @return <code>true</code> if this media element has a media
* trait of the specified class, <code>false</code> otherwise.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function hasTrait(type:String):Boolean
{
if (type == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
return traits[type] != null;
}
/**
* Returns the media trait of the specified type.
*
* @param type The MediaTraitType for the media trait to return.
*
* @throws ArgumentError If the parameter is <code>null</code>.
* @return The retrieved trait or <code>null</code> if no such trait exists on this
* media element.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getTrait(type:String):MediaTraitBase
{
if (type == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
return traits[type];
}
/**
* The media resource that this media element operates on.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get resource():MediaResourceBase
{
return _resource;
}
public function set resource(value:MediaResourceBase):void
{
_resource = value;
}
/**
* The media container that this element uses.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function get container():IMediaContainer
{
return _container;
}
/**
* Adds a Metadata object to this MediaElement under the specified namespace URL.
*
* @param namespaceURL The namespace URL used to store the Metadata.
* @param metadata The Metadata to add.
*
* @throws ArgumentError if namespaceURL or metadata is null.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function addMetadata(namespaceURL:String, metadata:Metadata):void
{
if (namespaceURL == null || metadata == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.NULL_PARAM));
}
this.metadata.addValue(namespaceURL, metadata);
}
/**
* Removes the Metadata object that was stored under this MediaElement with
* the specified namespace URL.
*
* @param namespaceURL The namespace URL of the Metadata to remove.
*
* @returns The removed Metadata, null if no Metadata with the given
* namespaceURL exists one this MediaElement.
*
* @throws ArgumentError If namespaceURL is null.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function removeMetadata(namespaceURL:String):Metadata
{
if (namespaceURL == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.NULL_PARAM));
}
return metadata.removeValue(namespaceURL) as Metadata;
}
/**
* Returns the Metadata object that is stored under this MediaElement with
* the specified namespace URL.
*
* @param namespaceURL The namespace URL of the Metadata object to retrieve.
*
* @returns The Metadata object with the specified namespace URL, null if
* no such Metadata object exists on this MediaElement.
*
* @throws ArgumentError If namespaceURL is null.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function getMetadata(namespaceURL:String):Metadata
{
if (namespaceURL == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.NULL_PARAM));
}
return metadata.getValue(namespaceURL) as Metadata;
}
/**
* A Vector containing the namespace URLs for all Metadata
* objects stored within this MediaElement.
**/
public function get metadataNamespaceURLs():Vector.<String>
{
return metadata.keys;
}
// Protected
//
/**
* @private
*
* Creates the metadata collection class.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function createMetadata():Metadata
{
return new Metadata();
}
/**
* @private
**/
public function get metadata():Metadata
{
return _metadata;
}
/**
* Adds a new media trait to this media element. If successful,
* dispatches a MediaElementEvent.
*
* @param type The MediaTraitType for the media trait to add.
* @param trait The media trait to add.
*
* @throws ArgumentError If either parameter is <code>null</code>, or
* if the specified type and the type of the media trait don't match,
* or if a different instance of the specific trait class has already
* been added.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function addTrait(type:String, instance:MediaTraitBase):void
{
if (type == null || instance == null || type != instance.traitType)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
// If there's a trait resolver for this type, then add the trait
// to the resolver:
var traitResolver:MediaTraitResolver = traitResolvers[type];
if (traitResolver != null)
{
traitResolver.addTrait(instance);
}
else
{
setLocalTrait(type, instance);
}
}
/**
* Removes a media trait from this media element. If successful,
* dispatches a MediaElementEvent.
*
* @param type The MediaTraitType for the media trait to remove.
*
* @throws ArgumentError If the parameter is <code>null</code>.
* @return The removed trait or <code>null</code> if no trait was
* removed.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function removeTrait(type:String):MediaTraitBase
{
if (type == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
var trait:MediaTraitBase = traits[type];
var traitResolver:MediaTraitResolver = traitResolvers[type];
if (traitResolver != null)
{
return traitResolver.removeTrait(trait);
}
else
{
return setLocalTrait(type, null);
}
}
/**
* @private
*
* Adds a trait resolver object for a given trait type.
*
* Only one resolver can be set per trait type.
*
* @param type The MediaTraitType for the media trait to add a resolver for.
* @param instance The resolver object to add.
*
* @throws ArgumentError If type is null, or if instance is null.
* @throws ArgumentError If a resolver has already been added for type.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
final protected function addTraitResolver(type:String, instance:MediaTraitResolver):void
{
if (instance == null || instance.type != type)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
if (traitResolvers[type] == null)
{
// Store the currently set local trait:
unresolvedTraits[type] = traits[type];
// Add the resolver to the trait resolvers dictionary:
traitResolvers[type] = instance;
// If there is a trait instance set for the added resolver's type,
// then add this trait to the resolver:
var currentlySetTrait:MediaTraitBase = traits[type];
if (currentlySetTrait)
{
instance.addTrait(currentlySetTrait);
}
// Run the resolved trait change routine: this will make sure that
// the local trait gets updated to reflect the resolved trait:
processResolvedTraitChange(type, instance.resolvedTrait);
// Listen for the resolved trait changing:
instance.addEventListener(Event.CHANGE, onTraitResolverChange);
}
else
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.TRAIT_RESOLVER_ALREADY_ADDED));
}
}
/**
* @private
*
* Removes a trait resolver object for a given trait type.
*
* @param type The MediaTraitType for the resolver to remove.
*
* @throws ArgumentError If type is null, or if no resolver has been added for type.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
final protected function removeTraitResolver(type:String):MediaTraitResolver
{
if (type == null || traitResolvers[type] == null)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
}
// Stop listening for the resolver's resolved trait value changing:
var instance:MediaTraitResolver = traitResolvers[type];
instance.removeEventListener(Event.CHANGE, onTraitResolverChange);
// Remove the resolver from the trait resolvers dictionary:
delete traitResolvers[type];
// Reset the unresolved trait to be the current local trait:
var unresolvedTrait:MediaTraitBase = unresolvedTraits[type];
if (unresolvedTrait != traits[type])
{
setLocalTrait(type, unresolvedTrait);
}
delete unresolvedTraits[type];
return instance;
}
/**
* @private
*
* Defines the trait resolver that's set for the specified trait type.
*
* @param type The MediaTraitType for the resolver object which should be returned.
* @return The requested resolver object, or null if none is set for the specified type.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
final protected function getTraitResolver(type:String):MediaTraitResolver
{
return traitResolvers[type];
}
// Subclass stubs
//
/**
* @private
*
* Sets up the trait resolvers for the media elements. Occurs during
* construction. Subclasses should override this method and call
* addTraitResolver for each trait they wish to add a resolver for.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function setupTraitResolvers():void
{
}
/**
* Sets up the traits for this media element. Occurs during
* construction. Subclasses should override this method and call
* addTrait for each trait of their own.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
protected function setupTraits():void
{
}
// Internals
//
private function onMediaError(event:MediaErrorEvent):void
{
dispatchEvent(event.clone());
}
private function setLocalTrait(type:String, instance:MediaTraitBase):MediaTraitBase
{
var result:MediaTraitBase = traits[type];
if (instance == null)
{
// We're processing a trait removal:
if (result != null)
{
// Stop listening for errors:
result.removeEventListener(MediaErrorEvent.MEDIA_ERROR, onMediaError);
// Dispose of any resources:
result.dispose();
// Signal removal is about to occur:
dispatchEvent(new MediaElementEvent(MediaElementEvent.TRAIT_REMOVE, false, false, type));
_traitTypes.splice(_traitTypes.indexOf(type),1);
delete traits[type];
}
}
else
{
// We're processing a trait addition:
if (result == null)
{
traits[type] = result = instance;
_traitTypes.push(type);
// Listen for errors:
result.addEventListener(MediaErrorEvent.MEDIA_ERROR, onMediaError);
// Signal addition:
dispatchEvent(new MediaElementEvent(MediaElementEvent.TRAIT_ADD, false, false, type));
}
else if (result != instance)
{
throw new ArgumentError(OSMFStrings.getString(OSMFStrings.TRAIT_INSTANCE_ALREADY_ADDED));
}
}
return result;
}
private function onTraitResolverChange(event:Event):void
{
var resolver:MediaTraitResolver = event.target as MediaTraitResolver;
processResolvedTraitChange(resolver.type, resolver.resolvedTrait);
}
private function processResolvedTraitChange(type:String, resolvedTrait:MediaTraitBase):void
{
if (resolvedTrait != traits[type])
{
setLocalTrait(type, resolvedTrait);
}
}
private function onContainerChange(event:ContainerChangeEvent):void
{
if ( _container == event.oldContainer
&& _container != event.newContainer
)
{
_container = event.newContainer;
}
}
private function onMetadataValueAdd(event:MetadataEvent):void
{
dispatchEvent
( new MediaElementEvent
( MediaElementEvent.METADATA_ADD
, false
, false
, null
, event.key
, event.value as Metadata
)
);
}
private function onMetadataValueChange(event:MetadataEvent):void
{
dispatchEvent
( new MediaElementEvent
( MediaElementEvent.METADATA_REMOVE
, false
, false
, null
, event.key
, event.oldValue as Metadata
)
);
dispatchEvent
( new MediaElementEvent
( MediaElementEvent.METADATA_ADD
, false
, false
, null
, event.key
, event.value as Metadata
)
);
}
private function onMetadataValueRemove(event:MetadataEvent):void
{
dispatchEvent
( new MediaElementEvent
( MediaElementEvent.METADATA_REMOVE
, false
, false
, null
, event.key
, event.value as Metadata
)
);
}
private var traits:Dictionary = new Dictionary();
private var traitResolvers:Dictionary = new Dictionary();
private var unresolvedTraits:Dictionary = new Dictionary();
private var _traitTypes:Vector.<String> = new Vector.<String>();
private var _resource:MediaResourceBase;
private var _metadata:Metadata;
private var _container:IMediaContainer;
}
}
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.