Infinite file size loading as3 - actionscript-3

I'm trying to load a big xml feed within ActionScript3. The problem is that the progress event indicate that the bytesTotal is zero and this result in a infinite loading sequence. The complete handler is never triggered.
This is what is do.
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadDone);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, dataAnalyzeProgress)
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, io_error);
var urlRequest:URLRequest = new URLRequest(url);
loader.load(urlRequest);
protected function io_error(event:IOErrorEvent):void
{
trace("IO ERROR")
trace(event.text)
}
protected function loadDone(event:Event):void
{
trace('DATA COMPLETE')
trace(event.target.content)
}
protected function dataAnalyzeProgress(e:ProgressEvent):void
{
trace((e.bytesLoaded / e.bytesTotal) *100+"%");
trace("Downloaded " + e.bytesLoaded + " out of " + e.bytesTotal + " bytes");
if(e.bytesTotal == 0)
{
loader.close();
}
}
Does somebody have a solution for this problem. I tried load it through curl i first, but still the same problem...

You need to use URLLoader class for loading xml data, not Loader. Loader class is for loading SWF and pictures (JPG, PNG, GIF). Try these lines:
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadDone);
loader.addEventListener(ProgressEvent.PROGRESS, dataAnalyzeProgress)
loader.addEventListener(IOErrorEvent.IO_ERROR, io_error);
var urlRequest:URLRequest = new URLRequest(url);
loader.load(urlRequest);

Related

AS3 Load Image Using Path From Text File

i use txt file that contain paths.
after loaded txt file and split to path array
then load img file from array path
but i get err in loading img
please help me
sample code:
var imglst:Array=new Array();
var lodimg:Loader=new Loader();
var lodtxt:URLLoader=new URLLoader();
lodtxt.load(new URLRequest("imglst.txt"));
lodtxt.addEventListener(Event.COMPLETE,onL_C);
function onL_C(e:Event)
{
var t:Array=new Array();
t=e.target.data.split(/\n/);
var s:URLRequest=new URLRequest(t[0].toString());
trace(t[0]);
lodimg.load(s);
}
lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
function onL_Cimg(e:Event)
{
var i:Bitmap=new Bitmap();
i=Bitmap(lodimg.content);
i.width=100;
i.height=100;
addChild(i);
trace("OK");
}
Are you loading images from a different website from your own? Does the other website(s) have a crossdomain.xml to allow permissions of loading their content? Usually Flash will give you a "security error" as an event but your code isn't listening for such an event so your program is not aware of any such problems... look on google how to handle AS3 errors.
Anyways a workaround is to just load the bytes of file using URLloader and on completion you then only use Loader to decode the bytes into pixel colours.
import flash.utils.ByteArray;
var imglst : Array;
var lodimg : Loader;
//var lodtxt : URLLoader = new URLLoader();
var lodURL:URLLoader = new URLLoader();
var img_bytes : ByteArray = new ByteArray();
lodURL.load(new URLRequest("http://yoursite/imglst.txt"));
lodURL.addEventListener(Event.COMPLETE,onL_C);
function onL_C (e:Event)
{
//# Since load complete no need to keep listening for that
lodURL.removeEventListener(Event.COMPLETE,onL_C);
var t:Array=new Array();
t=e.target.data.split(/\n/);
var s:URLRequest=new URLRequest(t[0].toString());
trace(t[0]);
//lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
//lodimg.load(s);
//# Now we know path so we load those file bytes
lodURL.dataFormat = URLLoaderDataFormat.BINARY;
lodURL.load(s); lodURL.addEventListener(Event.COMPLETE, onBytes_C);
}
function onBytes_C (e:Event)
{
//# on complete load of bytes...
trace("got bytes");
img_bytes = lodURL.data;
//trace("Image bytes total : " + img_bytes.length);
img_bytes.position = 0; //avoid "end of file" error
lodimg = new Loader();
lodimg.contentLoaderInfo.addEventListener(Event.COMPLETE,onL_Cimg);
lodimg.loadBytes(img_bytes); //we already have data so this will be fast
}
function onL_Cimg (e:Event)
{
var i:Bitmap = new Bitmap();
i = lodimg.content as Bitmap;
i.width=100;
i.height=100;
addChild(i);
trace("OK");
}

Flash-Animated-GIF-Library

I'm trying to use the Flash-Animated-GIF-Library. It serves to load an animated gif. It does it with the fileReference class, where you have to browse your folders, choose an animated gif and then it'll show it on the stage. I need the animated gif to show without that browsing part. Is it in anyway possible to use that class to load animated gifs directly, the same way you'd load and show an image with the Loader class? If so - how?
Yes, you have 2 options.
Use the Loader or URLLoader class. Example1, Example 2 (get bytearray)
Embed the image and get ByteArrayAsset. Example
The minimal code for option1 (Loader):
protected function handleCreationComplete(event:FlexEvent):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("yourgif.gif"));
}
private function loaderComplete(event:Event):void
{
    var loaderInfo:LoaderInfo = LoaderInfo(event.target);
    var byteArray:ByteArray = loaderInfo.bytes;     
player.loadBytes(byteArray);
}
The minimal code for option1 (URLLoader):
protected function handleCreationComplete(event:FlexEvent):void
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("yourgif.gif"))
}
private function loaderComplete(event:Event):void
{
player.loadBytes(event.target.data);
}
And for option2:
[Embed(source="yourgif.gif",mimeType="application/octet-stream")]
public var YourGif:Class;
protected function handleCreationComplete(event:FlexEvent):void
{
var byteArrayAsset:ByteArrayAsset = new YourGif();
player.loadBytes(byteArrayAsset);
// should work, too
//player.loadBytes(new YourGif() as ByteArray);
}

ActionScript 3: LoaderInfo COMPLETE event doesn't fire after load() and loadBytes()

I'm trying to load PNG images with ActionScript with a Loader object. This works fine for some of the images (the INIT and COMPLETE events are fired as expected), for some other it doesn't. I've read in this thread that a URLLoader might help, so I tried that, using the loadBytes() function afterwards. Still doesn't work: the URLLoader fires the COMPLETE event, but the LoaderInfo object does not.
I've written a sample class that demonstrates the problem with two files (one working, the other one not).
public class LoaderTest extends MovieClip {
var output:TextField;
var loader:Loader;
var urlLoader:URLLoader;
function LoaderTest() {
output = new TextField();
output.width = 1000;
output.height = 1000;
output.multiline = true;
addChild(output);
var t1:Timer = new Timer(0, 0);
t1.addEventListener(TimerEvent.TIMER, function() {
t1.stop(); loadMapDirect("map_in_big.png");
});
var t2:Timer = new Timer(1000, 0);
t2.addEventListener(TimerEvent.TIMER, function() {
t2.stop(); loadMapDirect("map_us_big.png");
});
var t3:Timer = new Timer(2000, 0);
t3.addEventListener(TimerEvent.TIMER, function() {
t3.stop(); loadMapBytes("map_in_big.png");
});
var t4:Timer = new Timer(3000, 0);
t4.addEventListener(TimerEvent.TIMER, function() {
t4.stop(); loadMapBytes("map_us_big.png");
});
t1.start();
t2.start();
t3.start();
t4.start();
}
function loadMapBytes(url:String):void {
try {
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(ProgressEvent.PROGRESS, progressListener);
urlLoader.addEventListener(Event.COMPLETE, completeListenerBytes);
output.appendText("\nLoading '"+url+"' with URLLoader ");
urlLoader.load(new URLRequest(url));
} catch (error:Error) {
output.appendText("Err: " + error.message + "\n");
}
}
function completeListenerBytes(e:Event):void {
output.appendText("COMPLETE Event fired for URLLoader!\n");
try {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);
output.appendText("Loading bytes with Loader ");
loader.loadBytes(e.target.data as ByteArray);
} catch (error:Error) {
output.appendText("Err: " + error.message + "\n");
}
}
function loadMapDirect(url:String):void {
try {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListenerDirect);
output.appendText("\nLoading '"+url+"' with Loader ");
loader.load(new URLRequest(url));
} catch (error:Error) {
output.appendText("Err: " + error.message + "\n");
}
}
function completeListenerDirect(e:Event):void {
var bmd:BitmapData = Bitmap(e.target.loader.content).bitmapData;
output.appendText("COMPLETE Event fired for Loader! => h: " + bmd.height + ", w: " + bmd.width + "\n");
}
function progressListener (e:ProgressEvent):void{
output.appendText(".");
if (e.bytesLoaded == e.bytesTotal) {
output.appendText(" progress complete, " + e.bytesTotal + " bytes loaded!\n");
}
}
}
All images were generated with the PHP GD library and I'm compiling with SWFTools's as3compile.
You can see the script it in action on http://www.wichte-sind-wichtig.de/as3loader/loaderTest.swf
The two images map_in_big.png and map_us_big.png are in the same folder (not allowed to post more hyperlinks).
Any ideas?
The problem is that your application is probably compiled for Flash Player 9. In version 9 the maximum allowed image dimensions are 2880 x 2800 and map_us_big.png is 3150 x 1570. I ran the application successfully when I compiled it for Flash Player 10.
Here's a reference http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#BitmapData%28%29
In AIR 1.5 and Flash Player 10, the maximum size for a BitmapData
object is 8,191 pixels in width or height, and the total number of
pixels cannot exceed 16,777,215 pixels. (So, if a BitmapData object is
8,191 pixels wide, it can only be 2,048 pixels high.) In Flash Player
9 and earlier and AIR 1.1 and earlier, the limitation is 2,880 pixels
in height and 2,880 pixels in width. If you specify a width or height
value that is greater than 2880, a new instance is not created.

URLLoader data to BitmapData

I'm trying to load an image file that's right next to the .SWF file. Something like this:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
trace(typeof(loader.data));
graphic = spritemap = new Spritemap(loader.data, 32, 32);
...
}
But this is the output I get:
object
[Fault] exception, information=Error: Invalid source image.
The thing is loader.data has the image's bytes but is not a BitmapData instance and that's what Spritemap is expecting.
How to convert to BitmapData?
Thanks
// define image url
var url:URLRequest = new URLRequest("http://sstatic.net/ads/img/careers2-ad-header-so.png");
// create Loader and load url
var img:Loader = new Loader();
img.load(url);
// listener for image load complete
img.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
// attaches the image when load is complete
function loaded(e:Event):void
{
var bitmap:Bitmap = e.target.content;
doStuffWithBitmapData(bitmap.bitmapData);
addChild(bitmap);
// remove listener
e.target.removeEventListener(Event.COMPLETE, loaded);
}
/**
* Handle loaded BitmapData
* #param bmd The loaded BitmapData
*/
function doStuffWithBitmapData(bmd:BitmapData):void
{
trace(bmd);
// your code
}
Basically;
You should be using Loader, not URLLoader. You can access the BitmapData of the loaded Bitmap with bitmap.bitmapData.

Flash As3 Loader Problem

Hi iam trying to load a few external jpegs in As3.
It works fine locally in Flash, but it dosen't work at all ion my server.
My app also loads a youtube video simultaneously.
function drawResult(index,videoID,song_title,thumbnail:String=null)
{
var theClip:resultRowClip=new resultRowClip ();
_clip.addChild(theClip);
myArray[index] = new Array(videoID,theClip);
theClip.y=0+(43*(index-1));
theClip.rowText.text = song_title;
theClip.rowBack.visible = false;
if (thumbnail != ""){
theClip.tHolder.visible=true;
loadImage(thumbnail,index);
}
}
function loadImage(url:String,index):void
{
//this.myClip.myText.text += "load image";
// Set properties on my Loader object
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (evt:Event){imageLoaded(evt,index)});
}
function imageLoaded(evt,id):void
{
//this.myClip.myText.text += "id : evt : " + evt.status;
// Load Image
var image:Bitmap = new Bitmap(evt.target.content.bitmapData);
myArray[id][1].tHolder.addChild(image);
myArray[id][1].tHolder.width=myArray[id][1].tHolder.width*0.35;
myArray[id][1].tHolder.height=myArray[id][1].tHolder.height*0.35;
}
Does anyone knows what the problem is ?
** I added two Evenet listeners from io Error :
imageLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
This is the function for handling the errors :
private function ioErrorHandler(event:IOErrorEvent):void {
this.myClip.myText.text +=("ioErrorHandler: " + event);
}
anyway, I got no errors...
I also tried to move the listeners before imageLoader.load but it's still the same...no errors and no data loaded..
I change my code to patrikS suggestion :
function loadImage(url:String,index):void
{
//this.myClip.myText.text += "load image";
// Set properties on my Loader object
//if (index != 1) return;
var imageLoader:Loader = new Loader();
imageLoader.name = index.toString();
//myArray[index][1].addChild(imageLoader);
//imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (evt:Event){imageLoaded(evt,index)});
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
imageLoader.load(new URLRequest(url));
}
My current completeHandler function(tnx patrikS ) :
private function completeHandler(evt:Event):void{
//this.myClip.myText.text += "id : evt : " + evt.status;
// Load Image
trace("evt target loader name : "+ evt.target.loader.name );
evt.target.removeEventListener(Event.COMPLETE, completeHandler );
var image:Bitmap = new Bitmap(evt.target.content.bitmapData);
myArray[evt.target.loader.name][1].tHolder.addChild(image);
myArray[evt.target.loader.name][1].tHolder.width=myArray[evt.target.loader.name][1].tHolder.width*0.35;
myArray[evt.target.loader.name][1].tHolder.height=myArray[evt.target.loader.name][1].tHolder.height*0.35;
//trace (hadar.y + "Y / X" + hadar.x);
}
It still work only on flash IDE and dosent work on any browser...
try urlstream and check crossdomain.xml :)
urlstream = new URLStream();
urlstream.addEventListener(Event.COMPLETE, onLoad);
urlstream.addEventListener(IOErrorEvent.IO_ERROR, onErr);
urlstream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onErr);
urlstream.load(req);
private function onLoad(e:Event):void {
var ba:ByteArray = new ByteArray();
urlstream.readBytes(ba, 0, urlstream.bytesAvailable);
_loader.contentLoaderInfo.addEventListener(Event.INIT, onBytesLoad);
_loader.loadBytes(ba);
}
Listeners should be added before calling the load() method.
Also there's no real advantage in using a closure for the complete event listener & think of removing the event listeners!
function loadImage(url:String, index:int):void
{
//this.myClip.myText.text += "load image";
// Set properties on my Loader object
var imageLoader:Loader = new Loader();
imageLoader.name = index.toString();
//make sure you to add your listeners here!
imageLoader.contentLoaderInfo.addEventListener(
IOErrorEvent.IO_ERROR,ioErrorHandler);
imageLoader.contentLoaderInfo.addEventListener(
Event.COMPLETE, completeHandler );
imageLoader.load(new URLRequest(url));
}
function completeHandler(event:Event ):void
{
//imageLoaded(evt,index);
trace( event.target.loader.name );
event.target.removeEventListener(Event.COMPLETE, completeHandler );
}
In debug model, you can load files from any avaliable site, but release model.
may be this help : http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html