Flash ExternalInterface in ASP.NET Applications

Update 11/27/2007 

We do quite a bit of Flash video integrated into ASP.NET web applications.  For video library application, the best solution from a usability perspective is to use JavaScript to control the flash player.  There are some issues that keep popping up with the Flash Player, SwfObject, ExternalInterface, and ASP.NET that we needed to document for the good of the world. Hopefully, we’ll save someone the hours that our team took to figure this stuff out.

First, we begin with some background. Adobe provides an interface called ExternalInterface that allows communication from a web page or via another application.  

The ActionScript is pretty straightforward shown in Figure 1 below.

import flash.external.ExternalInterface;

/* Register callbacks so we can call from JavaScript. */
ExternalInterface.addCallback(“Play”, null, playMe);
ExternalInterface.addCallback(“Pause”, null, pauseMe);
function playMe(String):Void {
    video.play(url);
}
function pauseMe():Void {
    video.pause();
}

Figure 1. ExternalInterface in Action(Script)

Once the interfaces are set up, you can call the methods externally, as shown below in JavaScript.

<script language=”JavaScript”>
           flashPlayer.Play(“/video.swf”);
</script>

Figure 2. JavaScript for Player calling method

This is all pretty straightforward stuff, but it doesn’t work within ASP.NET applications. 

The problem really comes down to the fact that every ASP.NET page is wrapped in a <form> tag and there is a bug in Internet Explorer that hides the containing flash object (the player) in the DOM. To solve this problem, you can simple elevate the player object to a window level element, as shown in Figure 3 below.

window.player = document.getElementById(‘player’);

Figure 3. Elevating the Flash Player object

Now you are really happy because your flash loads on the page. Then, your refresh the page and you get a JavaScript error. If this happens, I’ll bet it because you are using the SwfObject, like most people on the planet, are to embed Flash. There is a bug in SwfObject (in v1.4.4 and previous versions) or Internet Explorer when IE caches the Flash object. It appears that the interfaces are not re-instantiated on the cached flash object.

A good way to fix this is to put in a guid or random number into the call to the flash player as shown in Figure 4. below.

 var localplayer= ‘player.swf?guid=’ + Math.random()*99999999;
 var so = new SWFObject(localplaer, “player”, …);

Figure 4. Cache Busting

So, I believe that gives you all you need to know to navigate the tumultuous combination of Flash, ExternalInterface, SwfObject, IE and ASP.NET.


No Comments on Flash ExternalInterface in ASP.NET Applications

Comments on this entry are closed.