• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to transfer a local javascript AMF object from webpage to embedded swf file?

Community Beginner ,
Jul 24, 2012 Jul 24, 2012

Copy link to clipboard

Copied

Is it possible to transfer AMF data from the containing webpage into the embedded swf file (in that same webpage)?

For example, if I have a javascript ArrayBuffer in the webpage, and I encode into an AMF object (yes, this is another issue entirely), is there anyway to pass the AMF object to the swf file loaded in the webpage?

Everything I've seen so far is based on getting the AMF from a remote location, using say URLLoader, but in my case I already have the data locally, just not in the swf.

thanks

TOPICS
ActionScript

Views

2.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 25, 2012 Jul 25, 2012

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

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

{

    registerClassAlias("com.example.eg", ExampleClass);

    var eg1:ExampleClass = new ExampleClass();

    var ba:ByteArray = new ByteArray();

    ba.writeObject(eg1);

    ba.position = 0;

    ExternalInterface.call("onAMF", ba)

}

but in the javascript in the html page the returned object only has the following properties, bytesAvailable, endian, length, objectEncoding and position, and no binary data.

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.

thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

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.

If I pass the ByteArray to javascript, I just get the public properties.

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

what code are you using in your javascript to reconstruct eg1?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 31, 2012 Jul 31, 2012

Copy link to clipboard

Copied

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

amfByteArray.writeInt(9, 16);

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?

thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2012 Aug 01, 2012

Copy link to clipboard

Copied

anything that can be serialized can be passed using externalinterface and amf is serialized.

and it looks to me as if your javascript is receiving the data as an unrecognized data type object.  that's not a problem on the flash end though if you send the object back to flash it will be a problem because you're not registering the class correctly in flash, either.

http://books.google.com/books?id=XJrXl71TITIC&pg=PT487&lpg=PT487&dq=amf+data+externalinterface&sourc...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 01, 2012 Aug 01, 2012

Copy link to clipboard

Copied

LATEST

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.

With call mechanism, where the AS3 calls javascript and gets a return value, I got an xml parsing error (since xml can't handle buinary).

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines