Convert a Relative URL to an Absolute URL in Actionscript / Flex

Posted by Bear on Stack Overflow See other posts from Stack Overflow or by Bear
Published on 2010-01-05T01:23:46Z Indexed on 2010/06/06 7:02 UTC
Read the original article Hit count: 330

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.

  1. 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.

  2. Look at FlexGlobals.topLevelApplication, at the url or loaderInfo properties. Neither of these are set, however.

  3. 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://.

© Stack Overflow or respective owner

Related posts about flex

Related posts about url