How to detect if an embedded swf is created with Flex or Flash in javascript
Is there a way (in javascript) to detect if an embedded .swf was created with Flash Professional or Flex.
We have a page with several tabs, each of which can contain an .swf. All tabs are defined within the same HTML file and the javascript framework calls a .rewind() and .play() on the swf when the containing tab becomes active.
This works great 开发者_C百科on regular flash animation, making sure they start playing from the beginning when the tab is opened. On an swf created with Flex however, the rewind and play wreak havoc on the Flex framework and the application doesn't load.
The best way we've come up with to detect Flex is to count the number of frames the .swf has. With flex that's always 2. But this doesn't sound like the best way.
We've also tried to add a callback method with ExternalInterface on the Flex application preinitialize event. Unfortunately this event is called quite late in the application startup and the javasctipt code checks the callback before the Flex code has added it.
Is there any other way to detect (from javascript) if the .swf was created using Flex?
Short answer, no. Flex IS Flash; it's just an added layer on top of Flash to increase productivity in development.
However, there might be a solution for you, but I need to know if you created the swfs yourself and if you can modify them. If you can, I would use an ExternalInterface callback to 'play' your swf by calling that function in JS (ie. document.getDocumentById('swfId').someFunction();
).
The other solution would be to have all your swfs play immediately after load and then only load them in JS when needed (and not doing preloading). If you need to 'rewind', just reload them again (should be easy enough if you're using SWFObject, just remove and add back).
Ok, so, to answer my own question, DownloadProgressBar (loaded in the first frame) can be customized (overloaded).
So, this is the custom progress bar, that adds an isFlex() callback method to Javascript:
package flexidentifier {
import flash.external.ExternalInterface;
import mx.preloaders.DownloadProgressBar;
public class FlexIdentifierDownloadProgressBar extends DownloadProgressBar {
public function FlexIdentifierDownloadProgressBar() {
super();
ExternalInterface.addCallback("isFlex", callback);
}
protected function callback():String {
return "true";
}
}
}
Don't forget to add it to the Application:
<s:Application xmlns:s="library://ns.adobe.com/flex/spark" preloader="flexidentifier.FlexIdentifierDownloadProgressBar">
</s:Application>
It would have been nice if the people at Adobe put it in the Flex framework to start with.
精彩评论