Load external swf into bytearray with adobe flash - actionscript-3

How can I load external swf into bytearray with adobe flash AS3..
Here is my code..
var my_url1:URLRequest = new URLRequest("SWF/Lesson1.swf");
my_url1.method = URLRequestMethod.GET;
my_url1.contentType = "application/x-shockwave-flash";
var urlloader:URLLoader = new URLLoader(my_url1);
var myByteArray:ByteArray = new ByteArray();
urlloader.data = myByteArray;
It not works well. But it isn't give any error. How can I fix this problem?

You should listen for COMPLETE event and set the data format to BINARY:
var my_url1:URLRequest = new URLRequest("SWF/Lesson1.swf");
var urlloader:URLLoader = new URLLoader(my_url1);
urlloader.dataFormat = URLLoaderDataFormat.BINARY;
urlloader.addEventListener(Event.COMPLETE, function(event:Event):void
{
var myByteArray:ByteArray = URLLoader(event.target).data as ByteArray;
trace(myByteArray.length);
});

Related

Convert Actionscript 3 to Actionscript 2

I have an problem to convert AS3 to AS2
Here is my AS3 code
submit.addEventListener("mouseDown", sendData);
function sendData(evt:Event)
{
//for those using PHP
var students:URLRequest = new URLRequest("http://localhost/phpflash/new_student.php");
students.method = URLRequestMethod.POST;
var posts:URLVariables = new URLVariables();
posts.Fullname = Fullname.text;
students.data = posts;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(students);
}
Like #VC.One said, you must learn AS3, AS2 was dropped and actually Flash CC does not support it.
I do not code in AS2 since many, many years. So this code could contain some errors:
submit.onMouseDown = sendData;
function sendData():Void
{
var vars:LoadVars = new LoadVars();
vars.Fullname = Fullname.text;
var target:Object = new Object();
target.onLoad = function(){
trace("vars sended");
}
vars.sendAndLoad("http://localhost/phpflash/new_student.php", target, "POST");
}

How to get ByteArray of a Video?

I am using this example to load a FLV file to my project, it's very simple. It works, but I would like to get the bytes (as ByteArray) after I load the video. Is there any way I can perform this procedure?
For convenience, I was using the File object (Adobe AIR) to load the file, but have not found a way to convert the bytes to a Video object.
Does anyone have any idea how I can load a video file and, after loading, get the ByteArray of this object?
var connection:NetConnection = new NetConnection();
connection.connect(null);
var stream:NetStream = new NetStream(connection);
var client:Object = {};
client.onMetadata = function():void{};
stream.client = client;
var video:Video = new Video();
addChild(video);
video.attachNetStream(stream);
stream.play(null);
stream.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
var file:File = new File("path/to/your/video.flv");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
// this gives you access to the raw bytes of the file
var bytes:ByteArray = new ByteArray();
fileStream.readBytes(bytes);
// this adds the bytes to the NetStream and begins playback
stream.appendBytes(bytes);

bytearray to bitmap scenario

I just have a ByteArray, not the bitmap. I am using the below code to convert to Bitmap, but I end up getting end of file error.
//rstream is the ByteArray I have
var bytes:ByteArray = rstream;
var rect:Rectangle = new Rectangle(0,0,myPuzzle.width - 20,myPuzzle.height - 20);
var newBmd:BitmapData = new BitmapData(rect.width,rect.height,true,0xFFFFFFFF);
bytes.position = 0;
newBmd.setPixels(rect, bytes);
var image:Bitmap = new Bitmap(newBmd);
I got it working by using a Loader.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading1);
loader.loadBytes(rstream);
function loading1(e:Event):void
{
var image:Bitmap = Bitmap(e.target.loader.content);
myPuzzle.source = image;
}

Sending data from flash to php using post method as2

I am using following code in as3:
var Request:URLRequest = new URLRequest();
var xmlMain:XML=new XML();
var urlXmlLoader:Loader = new Loader();
var myVars:URLVariables = new URLVariables();
myVars.RequestXml = xmlMain;
Request.url = "http://www.xyz.php"; //some php url
Request.method = URLRequestMethod.POST;
Request.data = myVars;
urlXmlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlXmlLoader.addEventListener(Event.COMPLETE, getData);
urlXmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
urlXmlLoader.load(Request);
Now I have an as2 project where I need to do the same. How can it be done in as2??
This is probably the best tutorial on HTTP requests in AS2: http://www.sephiroth.it/tutorials/flashPHP/loadVars/

LocalConnection var into URLRequest

i use LocalConnection between two swf in the same page.
What i'm trying to do is to use a String sent from one swf to the other, as variable into my URLRequest...
So i can trace "myVar" into the function chemin, but i didn't find how to use it into URLRequest
thank's for your help
swf that receive the var :
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
function chemin(myVar:String){
trace(myVar)
}
var chargementXML:URLLoader = new URLLoader();
var fichier:URLRequest = new URLRequest(myVar);
Some references to help you out.
URLRequest.data: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html?filter_flex=4.1&filter_flashplayer=10.2&filter_air=2.6#data
URLVariables: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLVariables.html
So, if you want to pass some variables in your request, you do the following:
Create new URLVariables() object
Assign it's reference to URLRequest.data field.
var fichier:URLRequest = new URLRequest();
var urlVars:URLVariables = new URLVariables();
urlVars["myVarName"] = myVar;
fichier.data = urlVars;
This way your server side app will recieve a variable myVarName with value of myVar.
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
var receivedVar:String = "";
// somewhere else
function chemin(myVar:String){
receivedVar = myVar;
}
// later
var chargementXML:URLLoader = new URLLoader();
var vars:URLVariables = new URLVariables();
vars.myVar = receivedVar;
chargementXML.data = vars;
var fichier:URLRequest = new URLRequest(myVar);