Flash Player supported Thread in 11.5+.
I want to load an image by using Loader.loadBytes() in Worker Thread.
Which the Image ByteArray are generated in Main Thread.
But I can NOT do it.
I got a SecurityError like this:
SecurityError: Error #2123: Security sandbox violation:
Loader.content: file:///E:/work/ASWorkSpace/test/bin-debug/test.swf
cannot access
file:///E:/work/ASWorkSpace/test/bin-debug/test.swf/[[DYNAMIC]]/1. No
policy files granted access.
I init my worker thread like:
worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
so it's not a swf from remote, but it's local or like
[Embed(source="../workerswfs/Thread.swf", mimeType="application/octet-stream")]
I found in manual.
It said "If the loaded content is an image, its data cannot be accessed by a SWF file outside of the security sandbox, unless the domain of that SWF file was included in a URL policy file at the origin domain of the image."
I have gotten a solution. I loadByte() twice, it seems "washed off" the ByteArray source. so FlashPlayer considered the ByteArray generated by Worker Thread, and it allowed to access loader.content. Like this:
//Messages to the Main thread
protected function onMainToWorker(event:Event):void {
var msg:ByteArray = mainToWorker.receive() as ByteArray;
trace(msg.length);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, bytesComplete);
var bytes:ByteArray = new ByteArray();
bytes.writeBytes(msg,0,msg.length);
loader.loadBytes(bytes);
}
private function loadedAagin(e:Event):void
{
var loader:Loader = (e.target as LoaderInfo).loader;
// Here we can access it.
var bmp:Bitmap = loader.content as Bitmap;
}
private function bytesComplete(e:Event):void
{
var loader:Loader = (e.target as LoaderInfo).loader;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, bytesComplete);
// var bmp:Bitmap = loader.content as Bitmap; It can not be accessed
// just loadBytes again, it seems "washed off" the ByteArray source
var newloader:Loader = new Loader();
newloader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedAagin);
newloader.loadBytes(loader.contentLoaderInfo.bytes);
}
But it's so ugly isn't it?
Is there anyone has good idea?
Related
I am working on a AS3/Flash game and we are running into an issue when we load our home page swf into our login swf after someone successfully logs in.
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChildAt()
at fl.controls::BaseButton/drawBackground()
at fl.controls::LabelButton/draw()
at fl.controls::Button/draw()
at fl.core::UIComponent/drawNow()
at fl.controls::List/drawList()
at fl.controls::List/draw()
at fl.core::UIComponent/callLaterDispatcher()
We are developing in Flash Builder, importing a .swc with the artwork and components into our project. We load our homepage swf and add it as a display object like this:
private function LoadComplete(e:Event):void
{
//trace("LoadComplete");
m_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, LoadProgress);
m_homePage = e.target.content as DisplayObject;
}
Adding it:
addChild(m_homePage as DisplayObject);
Is there a better way to load a swf into another swf? Why would we be getting errors when loading the homepage swf through our login swf but not when we are debugging the home page separately?
Any advice would be very helpful.
If you want to execute external swf inside your own swf, you'll need to use SWFLoader
<mx:SWFLoader
id="sfwLoader"
width="100%"
height="100%"/>
And load code:
protected function loadSWF():void
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, onSWFLoaded);
loader.load(new URLRequest("app-storage:/myOther.swf")); // sample location
}
protected function onSWFLoaded(e:Event):void
{
var loader:URLLoader = URLLoader(e.target);
loader.removeEventListener(Event.COMPLETE, onSWFLoaded);
var context:LoaderContext = new LoaderContext();
context.allowLoadBytesCodeExecution = true;
context.applicationDomain = ApplicationDomain.currentDomain;
sfwLoader.loaderContext = context;
sfwLoader.addEventListener(Event.COMPLETE, loadComplete);
sfwLoader.load(loader.data);
}
protected function loadComplete(completeEvent:Event):void
{
var swfApplication:* = completeEvent.target.content;
}
This laods any swf, so it's potential security hole. Regards providing security you can examine: http://mabulous.com/air-applications-that-can-be-updated-without-requiring-admin-rights
My URL request is working perfectly in Flash Pro, but when I test it in the Browser, the image doesn't work at all. It doesn't load. Is there a Publish Setting I need to be using or something?
Please help, it's pretty frustrating. Here's what it looks like when it's in the browser.
Relevant code:
public function startSlidingPuzzle() {
// blank spot is the bottom right
blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
// load the bitmap
loadBitmap("http://fc07.deviantart.net/fs71/f/2014/030/d/7/slidingimage_by_nooodisaster-d74et6t.jpg");
}
// get the bitmap from an external source
public function loadBitmap(bitmapFile:String) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var request:URLRequest = new URLRequest(bitmapFile);
loader.load(request);
}
// bitmap done loading, cut into pieces
public function loadingDone(event:Event):void {
// create new image to hold loaded bitmap
var image:Bitmap = Bitmap(event.target.loader.content);
pieceWidth = image.width/numPiecesHoriz;
pieceHeight = image.height/numPiecesVert;
trace(numPiecesHoriz)
// cut into puzzle pieces
makePuzzlePieces(image.bitmapData);
// shuffle them
shufflePuzzlePieces();
}
You have a security error:
SecurityError: Error #2122: Security sandbox violation: Loader.content: http://fc03.deviantart.net/fs71/f/2014/030/e/e/beyonce_slidingpuzzle___flash_diary_day_10_by_nooodisaster-d74er8h.swf cannot access http://i.stack.imgur.com/ls9QI.jpg. A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.
at flash.display::Loader/get content()
at SlidingPuzzle/loadingDone()
Try adding a security context:
public function loadBitmap(bitmapFile:String)
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
context.securityDomain = SecurityDomain.currentDomain;
context.checkPolicyFile = true;
var request:URLRequest = new URLRequest( bitmapFile );
loader.load( request, context );
}
If that doesn't fix it you'll need to add a crossdomain.xml to the server hosting the images you are requesting. Why not save the images locally and deploy them in the same scope as your build?
[1]:
I have some questions with sharing/using/accessing variables/functions between loaded swf files.
my prj consists of main.swf file and 2 swf's which I load on first init of the main.swf.
my questions are:
1.how can I use variables from 1.swf in 2.swf (function is running in 2.swf)
2.how can I call a function from 2.swf in 1.swf
here is the code I'm using to load the swf's:
var playerMc:MovieClip = new MovieClip();
var dbMc:MovieClip = new MovieClip();
var m2Loader:Loader = new Loader();
var mLoader:Loader = new Loader();
startLoad();
function startLoad()
{
//var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("./_player/player.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc);
mLoader.load(mRequest);
addChild(mLoader);
//var m2Loader:Loader = new Loader();
var m2Request = new URLRequest("./_db/db.swf");
m2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc2);
m2Loader.load(m2Request);
addChild(m2Loader);
}
function loadMc(event:Event):void
{
if (! event.target)
{
return;
}
playerMc = event.target.content as MovieClip;
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc);
}
function loadMc2(event:Event):void
{
if (! event.target)
{
return;
}
dbMc = event.target.content as MovieClip;
dbMc.x = -400;
m2Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc2);
}
You have to stick with application domain.
In most cases you should load another swf in another application domain, but it's not really related to your question.
From loader, you must access to applicationDomain and then getDefinition. From there, you can get classes and use them in your main swf. Yes, you can read static properties.
If you need instances you should access loader#content. It is pointing to a root of loaded SWF. Root of loaded is SWF – is the instance of main class of the loaded swf.
Create a variable with no definition such as
public var MyClass;
as you can see i didnt add
public var MyClass:Class;
then in another function write
this.MyClass = this.mLoader.contentLoaderInfo.applicationDomain.getDefinition("NameOfClass") as Class;
i dont know much about this myself.. im having problems figuring out if you can only access Public static variables or if its possible to access normal public variables and possibly private variables because it is creating a new instance of the same class or however you want to word it..?
also after your write the above code .. when you want to change a varaibles this usually works for me
this.MyClass.RandomVariableName = this.MyClass.RandomVariableName + 1;
something like that..
I'm trying to create a screensaver for one our of our display we have at work. Images will be uploaded to an external server, from that server I will have pull the images and xml file. so my flash app and my content will be in two different places. I'm getting an error "SecurityError: Error #2000: No active security context". how do I override error and get the images to my stage.
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML;
var imageList:XMLList;
var imageLoader:Loader = new Loader();
var timer:Timer =new Timer(5000);
var imageIndex:uint = 0;
var child:DisplayObject;
var path:String="http://bgxserv03.mgmmirage.org/interactivemedia/mmhub01/test/mb/edit_bay/hr/infoscreen/servamb/";
xmlLoader.load(new URLRequest(path +"output.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
timer.addEventListener(TimerEvent.TIMER, tick);
function xmlLoaded(e:Event) {
xmlData = new XML ( e.target.data);
imageList = xmlData.image.name;
timer.start();
loadImage(imageList[0]);
}
function imageLoaded(e:Event){
if (child){
myImageHolder.removeChild(child);
}
child = myImageHolder.addChild(imageLoader);
Tweener.addTween(child, {alpha:0, time:1, delay:4});
Tweener.addTween(child, {alpha:1, time:1, delay:5});
}
function loadImage(path:String){
imageLoader.load(new URLRequest( path +"photos/"));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
}
Any help would be deeply appreciate. Thank you.
You need to put the "Crossdomain.XML" on the server's root directory. This will allow your flash file to access the data (image in your case) from that server. You can get a sample xml from the following URL, customize it for your server:
Sample CrossDomain.XML
What you are missing is probably a crossdomain.xml policy file at the domain of your image/xml files.
Use this link to create a crossdomain.xml file and add it to the root of your image/xml domain like so : "http://bgxserv03.mgmmirage.org/crossdomain.xml"
The URLLoader load() function automatically checks for the crossdomain.xml. Loader class requires you specify that you are interested in checking for a policy file in a LoaderContext object sent to the load() function.
In your code, it looks like the error should be coming from the URLLoader xml file request, since it doesn't look like you are trying to access the bitmap data of your images in any way, which is normally what would throw a security error for image files. If it is a problem with the image loading part, then complete the following instructions and you should be set to go:
In your loadImage function, add a LoaderContext parameter to your load method call:
function loadImage(path:String){
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.checkPolicyFile = true;
imageLoader.load(new URLRequest( path +"photos/"), loaderContext);
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageLoaded);
}
Check out the spec for more info on how to use the Loader class.
If you run into any trouble, this thread may be helpful.
Need your help. I am trying it first time.
I have used following code.
But i get this in my console:
started loading file
SecurityError: Error #2000: No active security context.
and my image url is in same folder as my script file.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.load(new URLRequest("C:\Documents and Settings\Owner\Desktop\23Aug\demo1\mic.jpg"), context);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
trace("started loading file");
addChild(loader);
function fileLoaded(event:Event):void
{
trace("file loaded");
}
Reasons to throw securityError exception.
Invalid path,
Trying to access a URL, that not permitted by the security sandbox,
Trying a socket connection, that exceeding the port limit, and
Trying to access a device, that has been denied by the user(Ex., camera, microphone.)
try this
private var _loader:Loader = new Loader();
private var _context:LoaderContext = new LoaderContext();
private var _url:URLRequest = new URLRequest("demo1/mic.jpg");
_context.checkPolicyFile = false;
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageloaded);
//_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_loader.load(_url, _context);
private function onImageloaded(e:Event):void{
addChild(e.target.content);
}
Use slashes instead of back slashes :
loader.load(new URLRequest("C:/Documents and Settings/Owner/Desktop/23Aug/demo1\mic.jpg"), context);
But the best way is to use relative path like "./mic.jpg" and do not use absolute path.