ActionScript® 3.0 Reference for the Adobe® Flash® Platform
Home  |  Show Packages and Classes List |  Packages  |  Classes  |  What's New  |  Index  |  Appendixes
org.osmf.media 

MediaFactory  - AS3 OSMF

Packageorg.osmf.media
Classpublic class MediaFactory
InheritanceMediaFactory Inheritance EventDispatcher Inheritance Object
Subclasses DefaultMediaFactory

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

MediaFactory represents a factory class for media elements.

The factory operation takes a MediaResourceBase as input and produces a MediaElement as output.

The MediaFactory maintains a list of MediaFactoryItem objects, each of which encapsulates all the information necessary to create a specific MediaElement. The MediaFactory relies on the canHandleResourceFunction method of each MediaFactoryItem to find a MediaFactoryItem object that can handle the specified MediaResourceBase.

The factory interface also exposes methods for querying for specific MediaFactoryItem objects, and for loading plugins (which hold MediaFactoryItem objects).

View the examples

Related API Elements



Public Properties
 PropertyDefined By
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  numItems : int
[read-only] The number of MediaFactoryItems managed by the factory.
MediaFactory
Public Methods
 MethodDefined By
  
Constructor.
MediaFactory
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
EventDispatcher
  
Adds the specified MediaFactoryItem to the factory.
MediaFactory
  
Returns a MediaElement that can be created based on the specified MediaResourceBase.
MediaFactory
 Inherited
Dispatches an event into the event flow.
EventDispatcher
  
Gets the MediaFactoryItem at the specified index.
MediaFactory
  
Returns the MediaFactoryItem with the specified ID or null if the specified MediaFactoryItem does not exist in this factory.
MediaFactory
 Inherited
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
  
Load a plugin identified by the specified resource.
MediaFactory
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
  
Removes the specified MediaFactoryItem from the factory.
MediaFactory
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
 Inherited
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Protected Methods
 MethodDefined By
  
Returns the most appropriate MediaFactoryItem for the specified resource out of the MediaFactoryItems in the specified list.
MediaFactory
Events
 Event Summary Defined By
 Inherited[broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active.EventDispatcher
 Inherited[broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive.EventDispatcher
  Dispatched when the MediaFactory has created a MediaElement.MediaFactory
  Dispatched when the MediaFactory has successfully loaded a plugin.MediaFactory
  Dispatched when the MediaFactory has failed to load a plugin due to an error.MediaFactory
Property Detail

numItems

property
numItems:int  [read-only]

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

The number of MediaFactoryItems managed by the factory.



Implementation
    public function get numItems():int
Constructor Detail

MediaFactory

()Constructor
public function MediaFactory()

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Constructor.

Method Detail

addItem

()method
public function addItem(item:MediaFactoryItem):void

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Adds the specified MediaFactoryItem to the factory. After the MediaFactoryItem has been added, for any MediaResourceBase that this MediaFactoryItem can handle, the factory will be able to create the corresponding media element. If a MediaFactoryItem with the same ID already exists in this factory, the new MediaFactoryItem object replaces it.

Parameters

item:MediaFactoryItem — The MediaFactoryItem to add.

Throws
ArgumentError — If the argument is null or if the argument has a null ID field.

createMediaElement

()method 
public function createMediaElement(resource:MediaResourceBase):MediaElement

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Returns a MediaElement that can be created based on the specified MediaResourceBase.

Returns null if the factory cannot find a MediaFactoryItem object capable of creating such a MediaElement in this factory.

Parameters

resource:MediaResourceBase — The MediaResourceBase for which a corresponding MediaElement should be created.

Returns
MediaElement — The MediaElement that was created or null if no such MediaElement could be created from the resource.

getItemAt

()method 
public function getItemAt(index:int):MediaFactoryItem

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Gets the MediaFactoryItem at the specified index.

Parameters

index:int — The index in the list from which to retrieve the MediaFactoryItem.

Returns
MediaFactoryItem — The MediaFactoryItem at that index or null if there is none.

getItemById

()method 
public function getItemById(id:String):MediaFactoryItem

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Returns the MediaFactoryItem with the specified ID or null if the specified MediaFactoryItem does not exist in this factory.

Parameters

id:String — ID of the MediaFactoryItem to retrieve.

Returns
MediaFactoryItem — The MediaFactoryItem with the specified ID or null if the specified MediaFactoryItem does not exist in this factory.

loadPlugin

()method 
public function loadPlugin(resource:MediaResourceBase):void

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Load a plugin identified by the specified resource. The MediaFactory will not reload the plugin if it has already been loaded. Upon successful loading, the MediaFactoryItems within the plugin's PluginInfo property will be added to this MediaFactory, and a MediaFactoryEvent.PLUGIN_LOAD event will be dispatched. OtherwiseIf the load fails, a MediaFactoryEvent.PLUGIN_LOAD_ERROR event will be dispatched.

Parameters

resource:MediaResourceBase — MediaResourceBase representing the plugin. For remote (dynamic) plugins, use an URLResource pointing to the remote SWF to load. For local (static) plugins, use a PluginInfoResource.

Throws
ArgumentError — If resource is null or resource is neither an URLResource nor a PluginInfoResource.

removeItem

()method 
public function removeItem(item:MediaFactoryItem):void

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Removes the specified MediaFactoryItem from the factory. If no such MediaFactoryItem exists in this factory, does nothing.

Parameters

item:MediaFactoryItem — The MediaFactoryItem to remove.

Throws
ArgumentError — If the argument is null or if the argument has a null ID field.

resolveItems

()method 
protected function resolveItems(resource:MediaResourceBase, items:Vector.<MediaFactoryItem>):MediaFactoryItem

Returns the most appropriate MediaFactoryItem for the specified resource out of the MediaFactoryItems in the specified list. This method is invoked when createMediaElement is invoked with a resource that more than one MediaFactoryItem can handle. Subclasses can override to select the most appropriate one. The default behavior is to select the first item which is not "native" to the framework, under the theory that plugins ought to take precedence over core media types. It makes this decision based on the presence or absence of an id value which starts with "org.osmf".

Parameters

resource:MediaResourceBase
 
items:Vector.<MediaFactoryItem>

Returns
MediaFactoryItem
Event Detail

mediaElementCreate

Event
Event Object Type: org.osmf.events.MediaFactoryEvent
property MediaFactoryEvent.type = org.osmf.events.MediaFactoryEvent.MEDIA_ELEMENT_CREATE

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Dispatched when the MediaFactory has created a MediaElement.

The MediaFactoryEvent.MEDIA_ELEMENT_CREATE constant defines the value of the type property of the event object for a mediaElementCreate event.

pluginLoad

Event  
Event Object Type: org.osmf.events.MediaFactoryEvent
property MediaFactoryEvent.type = org.osmf.events.MediaFactoryEvent.PLUGIN_LOAD

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Dispatched when the MediaFactory has successfully loaded a plugin.

The MediaFactoryEvent.PLUGIN_LOAD constant defines the value of the type property of the event object for a pluginLoad event.

pluginLoadError

Event  
Event Object Type: org.osmf.events.MediaFactoryEvent
property MediaFactoryEvent.type = org.osmf.events.MediaFactoryEvent.PLUGIN_LOAD_ERROR

Language Version: ActionScript 3.0
Product Version: OSMF 1.0
Runtime Versions: Flash Player 10, AIR 1.5

Dispatched when the MediaFactory has failed to load a plugin due to an error.

The MediaFactoryEvent.PLUGIN_LOAD_ERROR constant defines the value of the type property of the event object for a pluginLoadError event.
MediaFactoryExample.as

This sample demonstrates how MediaFactory can be used to load a plug-in. Note that the sample uses a mock-up plug-in URL.
package org.osmf.media
{
    import flash.display.Sprite;
    
    import org.osmf.events.MediaFactoryEvent;

    public class MediaFactoryExample extends Sprite
    {
        public function MediaFactoryExample()
        {
            // Construct a media factory, and listen to its plug-in related events:
            var factory:MediaFactory = new MediaFactory();
            factory.addEventListener(MediaFactoryEvent.PLUGIN_LOAD, onPluginLoaded);
            factory.addEventListener(MediaFactoryEvent.PLUGIN_LOAD_ERROR, onPluginLoadError);
            
            // Instruct the factory to load the plug-in at the given url:
            factory.loadPlugin(new URLResource("http://myinvalidurl.com/foo.swf"));        
        }
    
        private function onPluginLoaded(event:MediaFactoryEvent):void
        {
            // Use the factory to create a media-element related to the plugin:
            var factory:MediaFactory = event.target as MediaFactory;
            factory.createMediaElement(new URLResource("http://myinvalidurl.com/content"));
        }
        
        private function onPluginLoadError(event:MediaFactoryEvent):void
        {
            // Handle plug-in loading failure:
            trace("Plugin failed to load.");    
        }    
    }
}