actionscript3 (flash) don't load images form user file in chrome - actionscript-3

I'm trying to write flash applet where user can upload image from their hard disk.
So actionscript will edit image and send it to server.
Loading images work in flash player, firefox, and in opera, but in chrome after selecting image it stops.
I'm using flashdevelop.
Here is my code:
public class Main extends Sprite
{
[Embed(source = "../lib/lena.png")]
private var layer0Class : Class;
private var layer0:Bitmap = new layer0Class();
private var fileReferenceSelect:FileReference = new FileReference();
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
/// add image to flash scene
addChild(layer0);
/// add button
var my_button:SimpleButton;
my_button = new SimpleButton();
my_button.x = 150;
my_button.y = 50;
var cerchio:Shape=new Shape();
cerchio.graphics.beginFill(0x000000,1);
cerchio.graphics.drawCircle(my_button.x,my_button.y,20);
cerchio.graphics.endFill();
my_button.upState = cerchio;
my_button.overState = cerchio;;
my_button.downState=cerchio;
my_button.hitTestState = my_button.upState;
addChild(my_button);
/// button clicked
my_button.addEventListener(MouseEvent.CLICK,function(m:MouseEvent):void
{
fileReferenceSelect.browse([new FileFilter("PNG Files (*.png)","*.png; *.jpg; *.jpeg")]);
});
/// file selected
fileReferenceSelect.addEventListener(Event.SELECT, function(event:Event):void
{
fileReferenceSelect.load();
});
/// file ready to load
fileReferenceSelect.addEventListener(Event.COMPLETE, function(event:Event):void
{
var ldr:Loader = new Loader();
/// file loaded
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
var bm:Bitmap = Bitmap(e.target.content as Bitmap); /// here chrome is messing up
layer0.bitmapData = bm.bitmapData;
});
ldr.loadBytes(fileReferenceSelect.data);
});
}
}
Is it because of some limit in chrome (I read that flash in chrome is in sandbox) ?
Is there better way to do this ?

I've had issues loading flash content locally in chrome. Its the flash global player settings. The problem is, chrome has its own version of flash built in..I forget what its called..I think pepper flash? Anyway, it doesn't obey the settings all the time from this site: http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html
As long as you are testing on a web server it should be fine..heck even a local web server will work

Related

Load external SWF loader content not accessible

Actually i have a SWF that is loaded by another SWF.
This SWF that is loaded, itself load another SWF.
This may be weird, but it's in a context of an online game that let you develop SWF as plugins.
So, we have SWFA(the video game) ---> SWFB ----> SWFC
When i try to load the external SWFC, the COMPLETE event fire, but i am only able to add it to the stage by adding directly the Loader, because the loader doesn't have any content property.
public function load(){
loader = new Loader();
loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onIoError);
loader.contentLoaderInfo.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
applicationContext = new LoaderContext(true, ApplicationDomain.currentDomain);
loader.load(getRequest(),applicationContext);
}
private function getRequest():URLRequest {
Security.allowDomain("https://mock.com");
Security.loadPolicyFile("https://mock.com/crossdomain.xml");
var req:URLRequest = new URLRequest("https://mock.com/mock.swf");
req.method = URLRequestMethod.POST;
req.data = new URLVariables("name=kk");
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode("user:pass");
var credsHeader:URLRequestHeader = new
URLRequestHeader("Authorization", "Basic " + encoder.toString());
req.requestHeaders.push(credsHeader);
return req;
}
Here is the onComplete function
public function onComplete (e:Event):void{;
this.addChild(e.target.content); // this is null
this.addChild(e.currentTarget.content); // null too
}
So, when i run my SWF from my local computer, everything goes well, e.target.content contain my external SWF.
But when i run the same within the SWF Loader inside the online game, i can't find any content property. Plus, childAllowParent property of LoaderInfo is always false.
Does someone have an idea of what's going on?
Thanks

AS3 browse and load video

I'm currently working on a AS3 AIR project which involves allowing the user to browse a video file and load that same video onto the stage. I've managed to allow the user to browse for a video file type and according to my traces it completes loading the video but this is as far as I've got. There is plenty of tutorials which involve how to load video files from local sources or external links but nothing to show me what to do with a browsed file to display it on the stage. Here is the code so far for browsing to the video file:
private function browseVideo():void {
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, videoFileSelected);
var videoTypeFilter:FileFilter = new FileFilter("Video files", "*.3g2; *.3gp; *.asf; *.asx; *.avi; *.flv; *.m4v; *.mov; *.mp4; *.mpg; *.rm; *.swf; *.vob; *.wmv;");
fileReference.browse([videoTypeFilter]);
}
private function videoFileSelected(e:Event):void {
fileReference.addEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.addEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
fileReference.load();
}
function onVideoFileLoaded(e:Event):void {
var fileReferenceTarget:FileReference = e.target as FileReference;
var data:ByteArray = fileReferenceTarget["data"];
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
var videoLoader:Loader = new Loader();
videoLoader.loadBytes(data);
videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onVideoLoaderComplete);
trace("video file loaded");
}
function onVideoFileLoadError(e:Event):void {
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
trace("video file load failed");
}
function onVideoLoaderComplete(e:Event):void {
var loadedContent:DisplayObject = e.target.content;
var loader:Loader = e.target.loader as Loader;
scope.addChild(loader);
}
To play a video using AS3 ( Flash ) you can use a Video object on which you can attach a NetStream object, you can also use an FLVPlayback component. For flex, take a look on my answer of this question where I put an example of playing a video stream. And in all cases, I think that you don't need a FileReference object because a File is suffisant to get the path of your local file and then play it with any manner you want.
Take a look on this example :
function browseVideo():void {
var file:File = new File();
file.addEventListener(Event.SELECT, videoFileSelected);
file.browse([videoTypeFilter]);
}
function videoFileSelected(e:Event):void {
playVideo(e.currentTarget.nativePath);
}
function playVideo(video_path:String){
// using a Video + NetStream object
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = this;
var video:Video = new Video();
video.attachNetStream(ns);
addChild(video);
ns.play(video_path);
// using an FLVPlayback component inserted on the Stage
flvplayback.load(video_path);
flvplayback.play();
}
For more details on how to work with video, you can take a look here, you can find all what you need to know about video ( loading videos, supported formats, cue points, ... ).
Hope that can help.

How to load image to swf which is loaded into other swf? (AIR, AS3)

I have an application in AIR, which is loading swf files and makes previews of them. When those swfs loads other content (eg. jpg) then i have an error, that url not found - i am convinced that is about jpg file. This problem shows only when i load swf by filereference using loadBytes(). When i load swf by Loader using load() - problem doesn`t exist. How can i fix this issue? Sorry for my english. I attach code fragment with loading swfs.
private static function loadFile(loadType:String, swfNum:Number = 0):void {
lc = new LoaderContext();
lc.allowCodeImport = true;
lc.allowLoadBytesCodeExecution = true;
currentSwfNum = swfNum;
var fRef:FileReference = DataBase.swfsList[currentSwfNum];
fRef.addEventListener(Event.COMPLETE, onFileReferenceLoad);
fRef.load();
}
private static function onFileReferenceLoad(e:Event):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ifComplete);
loader.loadBytes(e.target.data, lc);
}
private static function ifComplete(e:Event):void {
var mc:MovieClip = LoaderInfo(e.target).content as MovieClip;
mc.addEventListener("SHOOT", SwfSingleCapture.captureSingleShoot);
}
DataBase.swfList - it is an array of FileReference objects, which are selected by FileReferenceList and browse().

Can't load image in swf

I get error with load image into swf file. Here is my code:
private var loader:Loader = new Loader( );
public function Battle()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"));
}
private function onComplete(event:Event):void
{
addChild(loader);
}
e very thing is OK, logo of Google appeared. But I need to manipulate pixel the image have loaded. I add a image Bitmap :
private var loader:Loader = new Loader( );
private var image:Bitmap;
public function Battle()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"));
}
private function onComplete(event:Event):void
{
image = Bitmap(loader.content);
this.addChild(image);
}
There is nothing appear. I can't find any solution on internet. I try and detect :
private function onComplete(event:Event):void
{
image = Bitmap(loader.content);
//after above line, rest of function never run
addChild(image); // <-- no run ##
trace("this no run"); // <-- no run ##
}
I use Flash Builder 4 and have no error or any warning.
Can somebody show me a solution, please?
Thank for reading.
You can't access pixel data of an image if image is in different domain than your application (and there is no crossdomain policy file)
1 You need crossdomain.xml on Google site :)
2 You need use checkpolicy:
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"), context);
but it works only if Google add crossdomain.
3 You can use little serverside proxy on your domain. Flash call proxy to get file with your url, proxy download it and return to flash...

Loader Complete event does not work with browser (AS3)

Im trying to load an image in a movie clip and change its size as follow:
var loader:Loader = new Loader();
public function setProfilePicture(url:String){
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplte);
loader.load(new URLRequest(url));
addChild(loader);
}
protected function onComplte(event:Event):void
{
EventDispatcher(event.target).removeEventListener(event.type, arguments.callee);
var image:DisplayObject = (event.target as LoaderInfo).content;
image.width = 132;
image.height = 132;
}
The code above works fine when I execute it with adobe flash CS5, but when I try to open it with a browser (e.g. Chrome), the size of images does not change to 132x132. I tried to put addChild(loader) in the onComplete function, but in this time when I open it with a browser, the image won't be even loaded, while executing with adobe flash CS5 remains as before.
My suggestion is that when we open it by browser, the function onComplete does not work, but WHY???
Any idea will be appreciated.
Try this hack:
protected function onComplte( event:Event ):void {
EventDispatcher( event.target ).removeEventListener( event.type, arguments.callee );
var loaderInfo:LoaderInfo = LoaderInfo( event.target );
loaderInfo.loader.scaleX = 132 / loaderInfo.width;
loaderInfo.loader.scaleY = 132 / loaderInfo.height;
}
check this link: actionscript3 (flash) don't load images form user file in chrome
Verify that it works in a different browser other than chrome. This is most likely the pepper flash problem in chrome
I played with your onComplte function and somehow the content property of LoaderInfo is not accessible from in there. If all else fail, the size of the image can still be controlled from within setProfilePicture by scaling the Loader:
public function setProfilePicture(url:String){
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplte);
loader.load(new URLRequest(url));
loader.scaleX = 10; ////
loader.scaleY = 10; ////
addChild(loader);
}