use the ExternalInterface class.
if you can use the call() method, you should have no problem. if you have to use the addCallback() method, you'll need to use compatible (with the ExternalInterface class) embedding code (like swfobject or the sample embed code shown in the flash help files).
Thanks for the response kglad. However I'm unable to get any sort of test to work. I have the following code in AS3, as a button click handler (I'm trying to pass AMF data out to JS as an initial test, in the end I'm going to have it the other way around though).
protected function AMFBtn_clickHandler(event:MouseEvent):void
var eg1:ExampleClass = new ExampleClass();
var ba:ByteArray = new ByteArray();
ba.position = 0;
Note: ExampleClass has no properties or methods (though I've tried with properties also and get the same result)
I'm using swfobject.embedSWF to embed the swf file, and all externalinterface communication works fine.
pass a string instead of a bytearray to test externalinterface. otherwise, you're testing a lot more than externalinterface.
then you start thinking about your serialization/deserialization code which is where you have a problem.
Passing anything through ExternalInterface, except binary, is fine. I also understand the serialization and deserialization has to be done manually on the javacript side, and that this is a bunch of work.
I also tried ByteArray.readUTFBytes() as the return, but it doesn' appear to be valid AMF.
I only see three possible ways to pass AMF data back and forth, ByteArray objects themselves, ByteArray.[read|write]Object, and ByteArray.[read|write]UTFBytes, but none seem to work.
I'd kill for some sample code showing AMF data (especially some binary) being passed through the ExternalInterface (either way) but I can't find anything at all.
Don't worry about the js side reconstruction, what I'm trying to do is get AMF data from the containing webpage to the embedded swf.
I'm trying to use JSAMF, https://github.com/torleif/JSAMF , with the following code, where data is the binary of a png image.
var amfByteArray = new amf.ByteArray();
amfByteArray.writeInt(0, 16); // add version
amfByteArray.writeObject(data); // add string, using data.length to determine it's length
amfByteArray.writeInt(0, 16); // end AMF file
var amf_data = amfByteArray.data;
_swfObj.pushAMFData(amf_data); // ExternalInterface
Not working of course, I still get a XML parsing error, which means ExternalInterface is not treating it as AMF.
kglad, I have to ask, how do you know ExternalInterface will handle AMF data?
1 person found this helpful
anything that can be serialized can be passed using externalinterface and amf is serialized.
http://books.google.com/books?id=XJrXl71TITIC&pg=PT487&lpg=PT487&dq=amf+data+externalinter face&source=bl&ots=X12hcBywKg&sig=z1Xkbqyj7mL_LezeGv4uMYJspjE&hl=en&sa=X&ei=2d0YUKizF-Xq6w GclYEQ&ved=0CFkQ6AEwAg#v=onepage&q=amf%20data%20externalinterface&f=false
I read the article, but that code (ExternalInterface.call("FlashXHRRespond", vars.answer) will only handle non-binary data (since it'll be marshalled using XML).
I used amfphp to send AMF data to flash, and using Charles I captured the AMF binary. I then loaded the binary into JS and passed it to the embeded swf file using both call and callback.
With callback mechanism, where JS calls the exposed AS3 method, passing the binary as the parameter, I got a string with an extra 5 bytes, but all the bytes were set to zero.
Does anyone have any idea why all the bytes would be set to zero? And why an extra 5 bytes was added? I see the amf from amfphp always has as 5 null bytes at the beginning...