Using RequestURL to get to a different folder - actionscript-3

I am using AS3 to make a game. I use RequestURL( filename ) to obtain the music I want to play. However, this means the music file has to be in the same folder as the .fla file. How can I use it to get a music file from a different folder? I don't really like having to leave my music outside of a Music folder or properly organised somehow....
EDIT: Should have mentioned this earlier. I tried to use simply something such as "whatever/filename", but I got this security error:
SecurityError: Error #2000: No active security context.
Why?

Have you tried using the flash.media.Sound.load() method yet?
//Make a new sound object
var s:Sound = new Sound();
//Load it
s.load(new URLRequest("someDifferentMusicFolder/loop.mp3"));
//Play it
s.play()
If you are loading it via another method, such as URLLoader + URLRequest, please post your code and we should be able to help a little better.
Generally speaking though URLRequests should allow you to specify different folders, so your problem may not be in the methodology you are using but may be in your implementation? Perhaps you are getting a security error? Whatever it may be, if you post your code we can help you narrow it down.

Related

How to load swf created with flipbook maker into flash

I'm trying to load the swf created by 'flip book maker' into my project.
the files created by flipbook maker are
book.swf
framework.swf
I used the following code
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("book.swf");
myLoader.load(url);
addChild(myLoader);``
the framework.swf is linked with book.swf, so it cannot run alone.
When I run the movie it shows the Error #2032, Stream error to framework.swf
book.swf and framework.swf both are in the same directory.They are generated by flip book maker What I needed is to load the book.swf into my project at a particular frame. I need help in AS3 for this purpose.
How to fix it..?
please help me..!
Error #2032 means that your Flash application could not load the framework.swf for some reason (you know this already of course). What you need to find out is why. Possible reasons are: you have the wrong URL, the framework.swf is not in the place you expect, there are crossdomain restrictions on the file you are trying to access ( you did not state where frameworks.swf).
What I would suggest doing is using a web debugging proxy tool like Fiddler (http://www.telerik.com/fiddler) or Firebug (is a Firefox extension) and take a look at the http requests to get more information on your error. You may find out that frameworks.swf is not in the location you expect.
This is just a IOError event triggered by Adobe Flash. Most likely the file just isn't found. Check your path and make sure you place the .swf file in your bin-debug directory if you're doing this from within Adobe Flash Builder

Can't find image exported from AIR app on Android phone

I feel as if I'm missing something very basic here. I'm trying to add an option to let export images from an Android app in PNG format. The code seems to work fine on my Droid Bionic and doesn't error out, but afterwards I can't find the image anywhere in the phone's storage. Where is it going? Is it possible to access images exported from an AIR app? Am I missing something completely obvious?
bdToSave.draw(screenshot);
var arrBytes:ByteArray = PNGEncoder.encode(bdToSave);
var file:File = File.desktopDirectory.resolvePath("testimage.png");
var fileStream:FileStream = new FileStream();
try {
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(arrBytes);
fileStream.close();
}
catch (errObject:Error) {
Env.outBox.text = "Error writing image file!"
}
I've also used the CameraRoll class to successfully export images and found the resulting files easily, but the images there seemed to use bad JPEG compression. So I was hoping using FileStream instead would result in nicer images.
File.desktopDirectory does not exist on Android. Use File.applicationDirectory or File.applicationStorageDirectory to write to the internal disk. You almost always want to use applicationStorageDirectory and I believe that is actually the only one you can use on iOS for writing data. For writing to external drives, and this is untested on my part, I believe you want to use File.documentsDirectory. Trace out File.documentsDirectory.nativePath to verify that is the correct path.
It might be wise to consider that the majority of higher-end phones being released now do not contain SD cards. If this is meant to be a mass market product, I'd recommend using applicationStorageDirectory instead.

How to read a song using an absolute location in AS3

I'm making a music player using AS3, but I'm having one problem; when I try to find a song outside the project folder using the AS3 script it simply doesn't work.
I've searched in the Internet and all the examples I've found used the relative location like this:
loadedSong = new Sound(new URLRequest("music.mp3"));
And it works for me, but with it I need always to have the songs in the root folder of the project. And I want to read them wherever they are without having to duplicate them to the root folder.
I tried to alter the code to use the absolute location like this:
loadedSong = new Sound(new URLRequest("c:\Users\pc\My Music\Artist\Album\music.mp3"));
But it fails with this error: "SecurityError: Error #2000: No active security context."
What am I doing wrong?
This is security restriction of Flash Player. If you need a desktop player, you'd better use Adobe AIR. Otherwise, you can use web server and store files there. Or you can just add allow Flash Player to read from hard drive by adding trusted locations here.

write/save xml in as3

I want to create a stand alone pure flash application with external xml loading from the same dir. In that i want to update/overwirite my xml nodes and save it internally at run time (I dont want to go for fileReference class to save/overWrite my xml). when I load my apps next time it will react based on the updated xml.
I dont want go for AIR as it requires installation at end user side.
I cant use any server side script.
Is it possible in flash AS3.0?
Please suggest any possible solution.
Thanks..
it's possible with FileReference.save()
here is an example saving .txt files
var file:File = File.userDirectory.resolvePath("test.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTFBytes("text to save");
fileStream.close();
last time I tried this worked without any prompt as to where to save . . . if it helps anyone.
(above worked with mobile devices saving to SDcard) - you will need to allow (WRITE_EXTERNAL_STORAGE) permission.
You can't access files in Flash without AIR and FileReference, but you can save data in SharedObject. This is analogous to cookies in browser (but not tied to one). Data will be saved somewhere in "C:\Users\UserName\Application Data\Macromedia\Flash Player\" directory (example for Win7) until user clears it or available disk size is exceeded.

Pass file from AS3 into embedded AS2 wrapper to load

I have a Flash AS3 application that uses FileReference.browse() to request a SWF from the user. If the chosen SWF is AS3, I'm good to go. However, if it's AS2, I need to load it into an AS2 wrapper first (so my app can alter it). All of these files (including my app and wrapper) are intended to exist locally on the user's machine, but the file they select can exist in any directory. So to be clear: Main application (AS3) -> Wrapper (AS2) -> User's file (AS2)
I know how to get the uploaded file's ActionScript version from the Loader's loaderInfo.actionScriptVersion variable, and that's working correctly. My issue is how to pass the file from the AS3 application to the AS2 wrapper so it can load it.
My first thought was to dump the ByteArray from the FileReference's load() function into a SharedObject "cookie". This method seemed pretty bad from a user-experience point of view, but it seemed most likely to work. However, I've been unable to find any method within AS2 to load the ByteArray as a movie (in fact, AS2 doesn't even seem to have a ByteArray class). So the first potential solution to my problem would be if anyone knew of a method for loading a movie from a ByteArray in AS2.
My second thought was to pass the uploaded file's path to my wrapper via the already-setup LocalConnection bridge, and then just have it load the file from that. However, I can't find any way to get the file's path, and my Googling suggests the security model intentionally prevents it. Not to mention, I'm not sure I can load an arbitrary file from the user's machine.
My "hands up in the air; I give up" solution was to just create separate buttons for loading AS3 and AS2 files (leaving it up to the users to guess which it is!) and have the AS2 button actually within the AS2 wrapper. However, it looks like AS2 doesn't have a file browsing uploading API, and the PHP-hybrid solutions I've found aren't an option (because this is meant to be run locally).
So, I would be eternally grateful if anyone could point me in the right direction for solving any of these three roadblocks. Alternative workarounds are of course welcome.
(Edit)
Ok, I found the documentation for AS2's version of the FileReference class. It supports the same file-browsing capability, but does not support directly loading the selected file into the SWF.
However, the security sandbox doesn't seem as strict for local files as I expected, and it looks like I can load any SWF on the user's machine once I have a path to it. So I should be able use JavaScript and an HTML form with a file input to get and pass the file path to my application. It's not ideal having to do all of this from within a web browser, but it should work. If it turns out satisfactorily I'll submit it as an answer.
(Edit 2)
Scratch the HTML-form idea. Looks like the path is hidden from JavaScript for the same reasons Flash hides it. The only option I can think of now is to have the user copy and paste the path to the file...
After reading over your post, you may be able to retry one of your previous attemps with some new information. Actionscript 2 DOES have a method for looking up files from a browser, same as AS3 does. AS2 also has a FileReference class. Check out the documentation here:
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001040.html
Also, here is a tutorial:
http://markshu.ca/imm/flash/tutorial/fileReference.html
Well, all of my other leads have dried up, so I'm submitting the two answers that will actually work, although neither is ideal:
A) Use Adobe AIR, which will give more access to the filesystem (such as for getting path info) at the cost of requiring the separate AIR runtime to be installed.
B) Have the user enter the path to the file themselves (cumbersome for the user)