开发者

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

开发者 https://www.devze.com 2022-12-15 16:20 出处:网络
I am working with Fle开发者_如何学JAVAx, and I need to take a relative URL source property and convert it to an absolute URL before loading it.

I am working with Fle开发者_如何学JAVAx, 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://.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号