Header Image - charles newman's blog

Category Archives

24 Articles

OSMF – Loading plug-ins locally with the file:/// protocol

Some of you may already know you can load an OSMF plug-in locally using the file protocol like this:

For Mac:


For Win:


This is very handy when developing a player because you don’t need to copy your player to localhost or a Web server to run and test.

However, I just discovered this oddity:
The plug-in will fail to load if the plug-in is a release build and the player is a debug build.

The failure happens down in the OSMF 1.5 core class DynamicPluginLoader in the onSWFLoaderStateChange method on this line:

var pluginInfo:PluginInfo = root[PLUGININFO_PROPERTY_NAME] as PluginInfo;

Flash Builder clearly shows the pluginInfo property on the root variable but the pluginInfo local variable is null after the above line executes.

This is not an OSMF bug, just something that you need to be aware of. I wasted about 30 minutes on this.

Dynamically loading a SWF and instantiating a Class in that SWF

There are a few blog posts out there on how to do this, but most of them involve a solution that requires a dummy reference in your Application code to the class you wish to instantiate, which forces the compiler to pull in the class, and therefore, allows getDefinition() to work.  But that solution defeats the whole purpose of loading a SWF dynamically.

If you want to load a SWF remotely, and then instantiate a class within that SWF at run-time, you are in the right place.

In the example below, the “application” is Foo and the class we’ll instantiate will be called Bar.  Let’s look at the SWF containing Bar first. Our goal, is to load the SWF that contains Bar and instantiate Bar in our application.


Below is the root level class of the SWF we’ll dynamically load, let’s say it’s called Dynamic.swf. Notice the private var of type Bar. This is necessary to get the compiler to pull the Bar class into our Dynamic.swf. The idea here, is we’ll build Dynamic.swf and put it up on a server somewhere, or localhost so we can test this.

	import com.charlespatricknewman.sample.Bar;

	import flash.display.Sprite;

	public class Dynamic extends Sprite
		// Need a dummy reference
		private var bar:Bar;

		public function Dynamic()


And here is the Bar class we want to instantiate in our Application at run-time:

package com.charlespatricknewman.sample
	public class Bar
		public function Bar()
			trace(">>> Bar() constructor!");

		public function doSomething():void
			trace(">>> Bar.doSomething() called!");

All this class does is trace out some text, but this is sufficient to prove this is working.

The Application

Next is Foo, the application that will load the SWF and instantiate Bar.  Note this project has no reference or dummy variables of type Bar.

Here is the root level class:

import com.charlespatricknewman.sample.DynamicClassLoader;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class Foo extends Sprite
	public function Foo()

	private function onAddedToStage(event:Event):void

	private function go():void
		var loader:DynamicClassLoader = new DynamicClassLoader();

		function setupListeners(add:Boolean=true):void
			if (add)

		function onClassLoaded(event:Event):void
			var Bar:Class = loader.instantiateClass(
			var bar:Object = new Bar();

And here is the DynamicClassLoader class you see referenced above. This class could reside in the Application SWF or a library (SWC) the application uses at compile time. It’s job is to load the SWF and allow the Application to instantiate a class in the loaded SWF.

package com.charlespatricknewman.sample
 import flash.display.Loader;
 import flash.errors.IllegalOperationError;
 import flash.events.Event;
 import flash.net.URLRequest;
 import flash.system.ApplicationDomain;
 import flash.system.LoaderContext;

 public class DynamicClassLoader extends Loader
	public function DynamicClassLoader()

	public function loadSWF(swfPath:String, classPath:String):void
		if (ApplicationDomain.currentDomain.hasDefinition(classPath))
			throw new IllegalOperationError("Class is already loaded!");

		var request:URLRequest = new URLRequest(swfPath);
		var context:LoaderContext = new LoaderContext();

		context.applicationDomain = ApplicationDomain.currentDomain;

		load(request, context);

	public function instantiateClass(classPath:String):Class
		return ApplicationDomain.currentDomain.getDefinition(classPath)
				as Class;

	private function onComplete(event:Event):void
		dispatchEvent(new Event(Event.COMPLETE));

Due to the security restrictions in the Flash Player, both Dynamic.swf and the Application SWF need to be loaded from either localhost or a web server. You might also need a crossdomain.xml file on the web server, but it’s best to try this from localhost first so you can see it working.

This might be a good solution if you have a SWF or a SWC that contains a large amount of code that is rarely used. Adding that code to your SWF or SWC might cause an unnecessarily large SWF. But loading that bloated Class only when you need it, will provide a much faster initial download and startup for your end users.

Building ActionScript 3 projects with Apache Ant

There are several reasons to use Apache Ant to build your ActionScript and Adobe Flex projects:

1) If your team uses Continuos Integration.

2) Your project has lots of dependencies that have version branches of their own.

3) You don’t want to shell out the bucks for Flash Builder.

4) You want to quickly build a project without having to import the project into Flash Builder.

There are quite a few blog posts out there on how to do this, so I’ll keep this short and simple so you can get started.

To start, download the zip file here, I’ll discuss the contents of the zip below. If you are impatient or simply don’t like to read, open a Terminal window, change to the directory where you unzipped the file and type “ant”, and press Enter. The script will build the project, create ASDocs, and then create a release zip file of the built project.

If you are on Windows, you’ll need to download and install Apache Ant.  You will also need the Flex SDK installed on your system, and the location of the SDK is specified in the “build.properties” file you will find in the zip.

The Code

Rather than use a “Hello World” app, we’ll start with an OSMF-based player that is as simple as it gets. This is the bare-bones code to play a video with the OSMF (Open Source Media Framework). You’ll find a ./src and a ./libs folder in the zip. The ./libs folder contains the OSMF.swc built for Flash Player 10.1. This is the same SWC file available at osmf.org.

The ./src folder contains one ActionScript file, OSMFSimple.as:

    import flash.display.Sprite;

    import org.osmf.elements.VideoElement;
    import org.osmf.media.MediaPlayerSprite;
    import org.osmf.net.StreamingURLResource;

    [SWF(backgroundColor="0x000000", frameRate="25", width="640", height="360")]

     * This is as simple as an OSMF-based media player can get.
    public class OSMFSimple extends Sprite
	private static const MEDIA:String =

	private var mediaPlayerSprite:MediaPlayerSprite;

	public function OSMFSimple()
		mediaPlayerSprite = new MediaPlayerSprite();

		mediaPlayerSprite.media =
                   new VideoElement(new StreamingURLResource(MEDIA));

The Ant Build Script

You will notice two files in the zip, “build.properties” and “build.xml”.

The build.properties file contains the settings that can be modified by developers to customize the build for their development environment. The idea here is: developers can tweak this file for their dev environments without having to touch the build script. This particular file contains only locale and the Flex SDK version location, but it could contain properties tied to environment variables, for example.


FLEX_HOME=/Applications/Adobe Flash Builder 4/sdks/4.1.0


The build.xml file is the build script. Download the zip file and take a look at the build.xml file. You will see tasks for cleaning the bin-release folder, cleaning the deploy folder, cleaning the ASDocs folder, creating the HTML wrapper files, building the release, building the ASDocs, and creating the zip file.

Notice at the top of file, in the project tag you see default=”main”. This tells Ant to look for a task named “main”. Now look at the bottom of file and you’ll see the main task. This simply kicks off the tasks required to build a release of this project.

To run the script, simply open a Terminal window, change to the directory, type “ant” and hit enter. Because the default task is defined as “main”, no other arguments are required.

If you wanted to run a particular task, you could supply that on the command line, such as:

ant release-build

You don’t need to name your Ant script “build.xml”. If you don’t, you need to supply the filename on the command line like this:

ant -f master-build.xml

And then to specify a task within that build script:

ant -f master-build.xml release-build

Run the script by opening a Terminal window, change to the folder where you unzipped the file, and type “ant”, hit Enter.  You should see output similar to this:

charles:OSMFSimple$ ant
Buildfile: /Users/charles/Downloads/OSMFSimple/build.xml

     [echo] cleaning...

     [echo] cleaning deploy folder...

     [echo] cleaning asdocs...

     [echo] building...
    [mxmlc] Loading configuration file /Applications/Adobe Flash Builder 4/sdks/4.1.0/frameworks/flex-config.xml
    [mxmlc] /Users/charles/Downloads/OSMFSimple/bin-release/OSMFSimple.swf (114460 bytes)


     [echo] building asdocs...
    [asdoc] Loading configuration file /Applications/Adobe Flash Builder 4/sdks/4.1.0/frameworks/flex-config.xml
    [asdoc] Documentation was created in /Users/charles/Downloads/OSMFSimple/docs/

      [zip] Building zip: /Users/charles/Downloads/OSMFSimple/deploy/OSMFSimple-release.zip


Total time: 17 seconds

If you were building a library (a SWC file) rather than a SWF, you would use the <compc> task rather than the <mxmlc> task.  For documentation on the Ant tasks supported by the Flex SDK see this page.

There are many other things you can do with Ant, such as sync with your Perforce depot, deploy to an FTP server, etc.

The only thing I haven’t figure out how to do with Ant (yet) is duplicate the “include source” check box in Flash Builder when exporting a release build. This is handy when creating sample projects. If anyone has figured this out please post a comment here.

I hope this helps you get started with ActionScript and Ant. It has made life much easier for our development team.

Flash Builder/Flex Builder – No Support for Relative Paths

You can’t enter a relative source path in Flex/Flash builder. For example, if I want the Flash authoring environment to find a class or classes on my system, I can tell it to look here: “../../libs” and it will go up 2 directories, go to libs and look in every directory under that until it finds the class. This allows me to check-in the FLA file and everyone on the team is going to be able to build it.

However, Flex/Flash Builder is built on Eclipse, and from what I’ve been a able to gather, relative paths are not accepted for source paths, which is a real pain. Unless everyone on your team stores code under “${DOCUMENTS}/everyone/uses/the/same/dev/path”, which in that case you could use the built in Linked Resources that Builder defines. But if you work on a framework or a public API, or just a dev team where people want choice over where they place their source code, this sucks.

I complained to one of the guys on the Builder team at Adobe about this and he told me this was a limitation of Eclipse and there is nothing they can do about it. But, he gave me a nice workaround and it occurred to me today, while showing this trick to a co-worker, that I should post this because others may not know about it.

Here’s the problem:

You create a new project in Flex/Flash builder and you want to reference the classes in another project. If that other project is a library, and you don’t want to reference the SWC, but the actual project source, you can simply hit “Add Project…” and select the project from the window that pops up and you are good to go, no relative paths required, Builder knows where everything is:

However, if you want to reference another project that is not a library project, such as an OSMF plugin (a project that creates a SWF, not a SWC), you need to use the “Source path” tab on this dialog and click the “Add Folder…” button:

Here’s the trick:

Don’t use the Browse button on the window that pops-up when you click “Add Folder…” above. Just type a slash (‘/’) followed by the project name.

Of course the project needs to be present in Builder (i.e., you either created it in Builder or imported it into Builder). The problem occurs when you use the browse button to locate the project because the entire path, which is specific to your system, will show up in the project files and no one except you is going to be able to build the project.  But if you just type a slash followed by the project name, builder will find the project and everyone will be able to build it.

In the image just above is what you should see on this tab, not the full path to the projects.

And your .actionScriptProperties file will have entries like this:


<compilerSourcePathEntry kind=”1″ linkType=”1″ path=”/AkamaiAdvancedStreamingPlugin”/>

<compilerSourcePathEntry kind=”1″ linkType=”1″ path=”/AkamaiBasicStreamingPlugin”/>


Notice the full path is not there.

One more thing:

You need to do one more thing before checking your code into a repository (SVN, Perforce, etc.):
Check your .project file and delete the <linkedResources> tag:
<name>[source path] AkamaiAdvancedStreamingPlugin</name>
<location>/Users/cnewman/Documents/Akamai/p4/projects/flash-client-dev/osmf/plugins/advanced-streaming-    plugin/trunk/AkamaiAdvancedStreamingPlugin</location>
Notice the full system path is in there. Builder doesn’t need this tag to build your project, it’s only for the Eclipse GUI, so just remove it from the file so no one gets errors importing your project.

I realize there are Linked Resources at the General->Workspace level for builder such as ${DOCUMENTS}. But this is not a great solution if you work on a project that a lot of people are going to need to be able to build, such as a public framework or API.  Everyone who wants to build your project is going to need to setup your required Linked Resource before they can build. But with the trick above, all they need to do is import your Builder project and go.

Flex Builder/Flash Builder – Switch Perspectives Quickly

I just discovered this by accidentally hitting the wrong key while debugging:

To switch from Debug Perspective back to Development Perspective, hit Cmd+F8.

Flex Builder pops up a list of Perspectives, hit Cmd+F8 again to cycle through your list.  This is very handy when you’re finished debugging and want to quickly get back to the Development Perspective without reaching for the mouse.  I occasionally stumble on these kinds of things I should already know…

Flex Builder Warning – “Source path entry, …, is a subdirectory of source path entry”

by Charles Newman

If you have two Flex projects sharing the same parent folder like this:



and ProjectTwo refers to ProjectOne in the “Flex Build Path” settings, you’ll get this warning:

Source path entry, ‘/Users/your-name/Documents/your-dev-path/ProjectOne’, is a subdirectory of source path entry, ‘/Users/your-name/Documents/your-dev-path/ProjectTwo/src’.

I don’t know about you, but compile warnings really bug me.  If there is really a problem, I’ll address it directly, but in this case, there is nothing wrong. So to remove this warning from your build, add this to your compile settings:


OSMF – Sprint 11 release now available (aka “FC1”)

Sprint 11 is the feature complete release of OSMF.  Lots of bugs were fixed for this release including the bug I mentioned in the Sprint 10 release blog post below about the MAST plugin and the SMIL plugin not working together.  In fact, in the AkamaiPluginSample all of the plugins are working nicely together.  This sample app has become the sample for demonstrating many of the OSMF plugins that reside in the OSMF repository.

For more details on this release, see this blog entry:


OSMF – Sprint 10 release now available for download

If you’ve been waiting for OSMF (Open Source Media Framework) to settle down, now is the time to dive in. This sprint was largely about locking down the API, but we added a few features as well:

  • DVR Support. OSMF now supports DVR via Flash Media Server’s server-side DVRCast application. This feature allows clients to jump back in time while viewing a live stream.
  • Syndication support (Atom 1.0, RSS 2.0, plus iTunes and Media RSS extensions).  You’ll see a new AS3 library in OSMF that can parse all of the major feed formats, plus the media-centric RSS extensions.  I’ve also updated the Akamai Plugin Sample app to load and parse an RSS feed and display a playlist.  If you load the MAST plugin before loading the RSS feed, you get a pre-roll ad before each video in the playlist. Note there is a bug with the MAST plugin and the SMIL plugin working together, I’m hoping to address this ASAP.
  • Extended HTTP Streaming Support. OSMF now supports subclips for HTTP-streamed content, and DRM for HTTP-streamed content.

See the OSMF blog post here for more details on these features:


As usual, if you have questions, you can post them to the OSMF forums:


Flex Builder 3 – How to get rid of the annoying unused CSS styles warning

by Charles Newman

If you like to use one CSS file in lots of different Flex projects you’ve probably seen an annoying warning message similar to this one:

“The CSS type selector ‘ComboBox’ was not processed, because the type was not used in the application. ”

This warning will appear at compile-time if you don’t have a ComboBox in your app, for example.  I like to put lots of styles in a few CSS files and reuse them, but I really don’t need this information. So the way to get rid of these warnings is to add this line to your “Additional compiler arguments” in the “Flex Compiler” section of your project’s properties:


OSMF – Sprint 9 release now available for download

The latest release of OSMF is now available here:


This is the work we did for Sprint 9, which concluded on January 20th. There are quite a few changes to the API and you can read the release notes for the specific changes.  The release notes have detailed descriptions of the API changes and should ease your pain when compiling your player against this new release.

Some of the new features in this release include:

See the OSMF blog post here for more details on these features:


We are currently heads down on Sprint 10, which will wrap up on February 24th.

As usual, if you have questions, you can post them to the OSMF forums: