22 February 2011
You should be familiar with the Flash Platform, building applications in Flash Professional or Flash Builder, using ActionScript 3, and concepts related to playing video using Flash.
Intermediate
This article begins a three-part series of hands-on development with Open Source Media Framework (OSMF). These articles provide a wide view of OSMF—what it has to offer and how to use it. It is geared towards developers or designers who have some development expertise and want to understand better how to take control of the power and benefits that OSMF has to offer.
Part 1 gets you started with the basics of OSMF and working with different media. Download, configure, and play back media with the Strobe Media Playback player—a precompiled, ready-to-deploy SWF file that is configured via the HTML source and the FlashVars.
Here's what we cover in the subsequent walkthroughs:
Start by downloading, configuring, and playing back media with the Strobe Media Playback player. The Strobe Media Playback player is a precompiled, ready-to-deploy SWF file that is configured via the HTML source and the FlashVars. Table 1 lists the currently supported FlashVars.
Table 1. Currently supported FlashVars in the Strobe Media Playback player
| Name | Value | Description |
|---|---|---|
src |
Location of the media file | |
loop |
true, false * |
Restart media when complete |
autoPlay |
true, false * |
Automatically start media |
controlBarPosition |
bottom *, none, over |
Position of the control bar |
backgroundColor |
Background color of the player | |
streamType |
liveOrRecorded *, live, recorded, dvr |
Media stream type. The DVR functionality only works if streamType = dvr. Otherwise you would only see the recorded part (if the mode is recorded) or the live part (if mode is live OR if mode is liveOrRecorded). |
autoHideControlBar |
true, false * |
Auto-hide the control bar |
scaleMode |
letterbox, none, stretch, zoom |
Controls how the media should be scaled |
* Default value.
As we go through the steps, we'll show you the basic use of some of the most common configuration properties, including loading dynamic plugins and basic namespaces for their metadata.
Follow these steps to download the player and open it in its default configuration:
Follow these steps to configure the player:
autoHideControlBar property and change this from false to true .controlBarPosition property from bottom to over .
parameters variable. This is where the Strobe Media Playback player is configured.src attribute is set to an HTTP path—currently, the media player is loading and playing a progressive video.
The Strobe Media Playback player also supports dynamic streaming. If the source is set to the URL for an F4M file, you can specify multiple streams and Strobe will switch between the streams as the user's bandwidth fluctuates in order to use the most efficient stream for the amount of bandwidth available. F4M is an XML file in a particular format, for which you can find the specifications on the OSMF website.
src attribute in the HTML to "http://mediapm.edgesuite.net/osmf/content/test/manifest-files/ dynamic_Streaming.f4m" .
It is also possible to do dynamic streaming using a SMIL file as the source. However, this requires you to load in the SMIL plug-in, which is available from the OSMF SVN repository. Fortunately, loading plug-ins with Strobe is the next subject we'll cover.
The following section illustrates how to configure the Strobe Media Playback player to load in an external plug-in from a remote location. It requires the use of a local or remote web server to properly test due to local FlashPlayer security:
plugin_simple: "http://osmf.realeyes.com/plugins/simple/ SimpleOSMFPlugin.swf"
simple_namespace: "http://osmf.realeyes.com/plugins/simple"
simple_message: "The plugin has loaded."
The code should look something like this:
var parameters =
{ id: "1"
, src: "http://mediapm.edgesuite.net/osmf/content/test/
logo_animated.flv"
, autoPlay: "true"
, width: "638"
, height: "400"
, autoHideControlBar: "true"
, controlBarPosition: "over"
, plugin_simple: "http://osmf.realeyes.com/plugins/simple/SimpleOSMFPlugin.swf"
, simple_namespace: "http://osmf.realeyes.com/plugins/simple"
, simple_message: "The plugin has loaded." };
Note: Plug-ins are configured by first naming the plug-in as 'plugin_simple'. (The '_' character separates the plug-in name definition as well as the property name for the value to be sent to the plug-in.)
alert method via ExternalInterface and passes the message defined by the plugin_simple property.Note: To load plug-ins, the StrobeMediaPlayback_config.html must be loaded from an "http://" URL, not directly from your file system. This is due to Flash Player cross-domain security. The video should play back after the JavaScript alert has appeared and OK is clicked to clear the alert.
The Strobe Media Playback component offers a powerful out-of-box solution that is easy to configure and deploy. Sometimes that's just not enough, however, and tighter integration to an existing application or in-depth customizing is required. In these cases, building a custom player might be your best solution. In the next section, you'll learn how to create a simple media player using OSMF to play back progressive, streaming FLV, and H.264 media.
In this walkthrough, you will create a very simple media player using the OSMF and the MediaPlayerSprite object. The purpose is to see how to create the simplest form of an OSMF player and play back progressive, streaming FLV, and H.264 media.
Follow these steps to set up the player:
Note: This file has been provided as a starting point for these walkthroughs.
Follow these steps to build the player:
"DECLARATION" comment, create a public variable named playerSprite that is typed as a MediaPlayerSprite object:public var playerSprite:MediaPlayerSprite;
initPlayer method. This method is called when the media player starts up.initPlayer method, set the playerSprite property equal to a new MediaPlayerSprite object:playerSprite = new MediaPlayerSprite();
resource that is typed as a URLResource object. Set this equal to a new URLResource object and make sure you pass the static const variable PROGRESSIVE_PATH as the only parameter to the constructor:PROGRESSIVE_PATH as the only parameter to the constructor:
playerSprite = new MediaPlayerSprite();
var resource:URLResource = new URLResource( PROGRESSIVE_PATH );
media property of the playerSprite object equal to a new VideoElement object, passing the resource variable as the only parameter to the constructor:playerSprite = new MediaPlayerSprite();
var resource:URLResource = new URLResource( PROGRESSIVE_PATH );
playerSprite.media = new VideoElement( resource );
addChild method, passing it the playerSprite :playerSprite = new MediaPlayerSprite();
var resource:URLResource = new URLResource( PROGRESSIVE_PATH );
playerSprite.media = new VideoElement( resource );
addChild( playerSprite );
initPlayer method should look like this:protected function initPlayer():void
{
playerSprite = new MediaPlayerSprite();
var resource:URLResource = new URLResource( PROGRESSIVE_PATH );
playerSprite.media = new VideoElement( resource );
addChild( playerSprite );
}
STREAMING_PATH . Notice that this path starts with "rtmp://" and does not have a file extension. Save the file and run it as a web application. Now the Akamai Anniversary video should play (see Figure 7). The same code used for the progressive video playback works great for streaming media.
STREAMING_MP4_PATH. Make note of the "mp4:" that is part of this path and that this path does have a file extension. When this version is run, the streaming H.264 Elephants Dream video should play (see Figure 8).
With OSMF, building a simple player with a lot of power is easy, but rarely is it enough just to show video. Often, finer-tuned control of the media loading and display process is needed. The next walkthrough will show how to separate some of the OSMF internals to provide greater control and understanding of the moving parts within a media player.
In this walkthrough, you will break out the main control objects of an OSMF media player. You will learn about the different components available to create an OSMF experience and how they can be managed independently instead of using the all-in-one MediaPlayerSprite object. In the end, it all works the same; but now there are separate instances to control the connection, playback, and visual display.
This walkthrough will demonstrate the use of the following objects:
Follow these steps to set up the player:
Follow these steps to break out the main control objects:
public var playerSprite and replace it with two variables: player , typed as a MediaPlayer object, and container , typed as a MediaContainer object:public var player:MediaPlayer;
public var container:MediaContainer;
initPlayer method, remove the line that created the playerSprite object as well as the other playerSprite references—including the addChild method. The initPlayer method should look similar to this: protected function initPlayer():void
{
var resource:URLResource = new URLResource( STREAMING_MP4_PATH );
}
resource variable, create a new local variable named netLoader and set it equal to a new NetLoader object. The NetLoader object enables you to have more control over the connection made to load the media. var resource:URLResource = new URLResource( STREAMING_MP4_PATH );
var netLoader:NetLoader = new NetLoader();
element and pass the VideoElement's constructor the resource and netLoader variables as parameters: var resource:URLResource = new URLResource( STREAMING_MP4_PATH );
var netLoader:NetLoader = new NetLoader();
var element:VideoElement = new VideoElement( resource, netLoader );
Now, when the VideoElement is created, it will use your explicit NetLoader.
player property equal to a new instance of the MediaPlayer object. Make sure to pass the element variable to the constructor: var element:VideoElement = new VideoElement( resource, netLoader );
player = new MediaPlayer( element );
container property equal to a new instance of the MediaContainer object, and then call the addMediaElement method on the container property, passing it the element variable: var element:VideoElement = new VideoElement( resource, netLoader );
player = new MediaPlayer( element );
container = new MediaContainer();
container.addMediaElement( element );
addChild method and pass the container property to the addChild method. The completed initPlayer method should look like this: protected function initPlayer():void
{
var resource:URLResource = new URLResource( STREAMING_MP4_PATH );
var netLoader:NetLoader = new NetLoader();
var element:VideoElement = new VideoElement( resource, netLoader );
player = new MediaPlayer( element );
container = new MediaContainer();
container.addMediaElement( element );
addChild( container );
}
Separating out the different controllers within a media player opens up the door to truly taking control of the capabilities of the player and the user experience when customization is needed. Toward that same goal, it lays the foundation for extending the implementation to take fuller advantage of the power of OSMF. Taking what has been completed already and adding support for any of the media supported by Flash Player (images, audio, video, and other SWFs) is the next step in harnessing the power of OSMF.
In this walkthrough, you will extend the media player so it can handle different types of media. This will show you how to use a MediaFactory and the generic MediaElement so the player can handle any of the supported media types without having to know what kind of media is being loaded. This way the player isn't tied to a VideoElement, for instance, that won't be able to play a SWF file or a dynamic multi-bitrate stream.
Follow these steps to set up the player:
In this start file we added two additional paths for media: one that is a SWF file and another that is an F4M file for Dynamic Streaming. Try to play the SWF file in the media player's current state.
STREAMING_MP4_PATH to LOCAL_SWF .mediaFactory and typed as a DefaultMediaFactory object:public var player:MediaPlayer;
public var container:MediaContainer;
public var mediaFactory:DefaultMediaFactory;
initPlayer method, remove the line that creates the netLoader object. Replace that line with a line that sets the mediaFactory property equal to a new instance of a DefaultMediaFactory object:var resource:URLResource = new URLResource( LOCAL_SWF );
mediaFactory = new DefaultMediaFactory();
element variable with code that calls the createMediaElement method on the mediaFactory property—you will need to pass the resource variable to the createMediaElement method. You will also need to change the variable type from VideoElement to MediaElement.Note: The MediaFactory will generate the appropriate MediaElement for the content dynamically for you, thus abstracting the content type from the player capabilities.
Before:
var element:VideoElement = new VideoElement( resource, netLoader );
After:
var element:MediaElement = mediaFactory.createMediaElement( resource );
LOCAL_SWF variable passed to the URLResource constructor with the DYNAMIC_STREAMING variable. This should play the dynamic stream defined by the loaded F4M file (see Figure 10).
Note: If you'd like to see how to manually define a dynamic streaming resource, a sample has been provided in the DynamicStreamSample.as file.
In this tutorial, you learned many of the basics of working with the Open Source Media Framework. Understanding how to implement a media player and the core parts that can be separated to enhance the control and leverage the full spectrum media type support is just the first step on the path to mastering OSMF.
Part 2: Creating and customizing your player focuses on building the UI with the help of OSMF, and extending the media experience with compositions to handle more in-depth use cases and start to really let the power of OSMF shine.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.
| 04/23/2012 | Auto-Save and Auto-Recovery |
|---|---|
| 04/23/2012 | Open hyperlinks in new window/tab/pop-up ? |
| 04/21/2012 | PNG transparencies glitched |
| 04/01/2010 | Workaround for JSFL shape selection bug? |
| 02/13/2012 | Randomize an array |
|---|---|
| 02/11/2012 | How to create a Facebook fan page with Flash |
| 02/08/2012 | Digital Clock |
| 01/18/2012 | Recording webcam video & audio in a flv file on local drive |