views:

570

answers:

1

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

A: 

I believe you just need to put the currentDirectory property of your application's invoke event in front of the relative part of your URL to get the absolute URL. Note that this could still contain things like ./ and ../ within it.

Here's some code from the relavant Adobe livedocs page:

var arguments:Array;
var currentDir:File;
public function onInvokeEvent(invocation:InvokeEvent):void {
    arguments = invocation.arguments;
    currentDir = invocation.currentDirectory;
}
whybird
Hey whybird, thanks for lending a hand. I'll need to check it out tomorrow, but it looks like the onInvokeEvent is only fired for AIR application launch. This will need to work for web applications as well. If possible, I'd also like to avoid adding a listener if the information is otherwise available. I'll tinker around a bit and see what pops up.
Bear
Yeah, I understand about the listener, although in this case you can kill it after it happens once. I believe onInvokeEvent works on web app launch too. Check the last bullet point under the "Application Invocation" heading in the livedoc linked above.
whybird