Convert a Relative URL to an Absolute URL in Actionscript / Flex
- by Bear
I am working with Flex, and I need to take a relative URL source property and convert it to an absolute URL before loading it.
The specific case I am working with involves tweaking SoundEffect's load method. I need to determine if a file will be loaded from the local file system or over the network from looking at the source property, and the easiest way I've found to do this is to generate the absolute URL.
I'm having trouble generating the absolute URL for sound effect in particular. Here were my initial thoughts, which haven't worked.
Look for the DisplayObject that the Sound Effect targets, and use its loaderInfo property. The target is null when the SoundEffect loads, so this doesn't work.
Look at FlexGlobals.topLevelApplication, at the url or loaderInfo properties. Neither of these are set, however.
Look at the FlexGlobals.topLevelApplication.systemManager.loaderInfo. This was also not set.
The SoundEffect.as code basically boils down to
var url:String = "mySound.mp3";
/*>> I'd like to convert the URL to absolute form here and tweak it as necessary <<*/
var req:URLRequest = new URLRequest(url);
var loader:Loader = new Loader();
loader.load(req);
Does anyone know how to do this? Any help clarifying the rules of how relative urls are resolved for URLRequests in ActionScript would also be much appreciated.
edit
I would also be perfectly satisfied with some way to tell whether the url will be loaded from the local file system or over the network. Looking at an absolute URL it would just be easy to look at the prefix, like file:// or http://.