March 18th, 2009 Goto comments Leave a comment
The External API allows an ActionScript developer to easily interact with the container program that is hosting Flash Player 8 and vice versa. The majority of the time, this will most likely be a Web browser, but this does not always have to be the case.
As many C# developers know, it is easy to house an ActiveX control (the IE version of Flash Player) in a .NET Windows application. This means we can now load an SWF in our Windows application and easily send data back and forth. Keep in mind that the keyword in this statement is “easily;” although possible before, it was not nearly as simple as the External API makes it now!
C# to ActionScript Communication
As I said before, communication between Flash Player and its container has been made extremely easy. The new class that makes this process so easy is the ExternalInterface. We will begin in the ActionScript. First, we need to import this new class so we can use it (as2 only, in as3 it will work without the import):
import flash.external.ExternalInterface;
Next, we have to register any function we want to make available externally:
ExternalInterface.addCallback("addText", addText);
Basically, the code above will allow us to call the addText function (which I will show in a minute) from the C# application.
The addText function is as below. Basically, it takes a string input and appends it to a text box
function addText(val:String):void
{
inTxt.appendText(val + "\n"); // append text recieved from c#
}
That’s it from the ActionScript side. Now all we need to do is call the function from C#. First, I add an instance of the Flash Player ActiveX control to my form and load the SWF we created in the form’s constructor:
private AxShockwaveFlash player;
public DemoForm ()
{
...
player.LoadMovie(0, Application.StartupPath + "\\EITest.swf");
player.Play();
...
}
Next, all we have to do is call the externalized method when desired. In my case, it is in response to the user clicking the send button:
private void sendBtn_Click(object sender, EventArgs e)
{
player.CallFunction("" + outTxt.Text + "");
}
ActionScript to C# Communication
Again, you will need to use the ExternalInterface in the ActionScript:
function send(evt : Event):void
{
ExternalInterface.call("sendText", outTxt.text); // function to call and it's parameters
outTxt.text = ""; // reset text box
}
As you can see, I am calling a method sendText and passing the input string as a parameter. Now to receive the message in C#, we first have to subscribe to the FlashCall event. You can do this in the constructor or from the activex properties panel on events tab.
Now the call made in ActionScript will be received in the request property of the event argument. For my particular call, the XML will look like this:
So now all we have to do is parse the XML in the event handler and invoke the C# function locally:
private void player_FlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
{
// message is in xml format so we need to parse it
XmlDocument document = new XmlDocument();
document.LoadXml(e.request);
// get attributes to see which command flash is trying to call
XmlAttributeCollection attributes = document.FirstChild.Attributes;
String command = attributes.Item(0).InnerText;
// get parameters
XmlNodeList list = document.GetElementsByTagName("arguments");
// Interpret command
switch (command)
{
case "sendText" : resultTxt.Text = list[0].InnerText; break;
case "Some_Other_Command" : break;
}
}
Viola!
I have made the simple example discussed available here.
ArrayArrayArrayArrayArrayArrayArrayArrayArray
Related posts:
Right click and custom context menu in Flash/Flex
Use clipboard (copy/paste) in C# console application
How to identify at runtime if the swf is in debug or release mode/build
How to clone (duplicate) an object in ActionScript 3
How To – Center a image on a canvas in flex
C#, Programming actionscript, activex, C#, communication, external interface
Comments (12) Trackbacks (1) Leave a comment Trackback
wow power leveling April 18th, 2009 at 04:13 | #1 Reply | Quote Thanks for the great resource
Pankaj May 11th, 2009 at 17:33 | #2 Reply | Quote It is nice work,
I am new to Flex.
It would be great if you can share the Action Script file as well, i.e the source code of SWF file as well.
Thanks.
Pankaj
admin May 12th, 2009 at 08:18 | #3 Reply | Quote @Pankaj
it’s there in the zip file under Flash Project folder
Pankaj May 13th, 2009 at 15:01 | #4 Reply | Quote Hi,
In the Flash Project, there are two files .swf and .fla
what I needed was some .as file or .msxml file which I can open on Flex Builder and see the code behind that flash file.
Thanks
Pankaj
admin May 13th, 2009 at 15:11 | #5 Reply | Quote @Pankaj
csharp.as
——————–
package
{
import flash.external.ExternalInterface;
public class Csharp
{
private var _text : TextArea;
public function Csharp(someTextArea : TextArea)
{
this._text = someTextArea;
ExternalInterface.addCallback(“addText”,addText);
}
// get message from c#
private function addText(val:String):void
{
_text.appendText(val + “\n”);
}
// Send message to C#
public function send(message : String):void
{
ExternalInterface.call(“sendText”, message);
}
}
}
the way to use it is
var com : Csharp = new Csharp(responseBox); // where responseBox is a textarea where messages will be apended when recived from c#
and if you want to send someting you do
com.send(‘hello from flash’);
that’s it. the code was in the article, rest of it was just…interface
Lucas June 18th, 2009 at 01:16 | #6 Reply | Quote Hey man, this works perfect, but what if I want to send more than 1 data to C# with the same function. Do I have to put several ExternalInterface.call inside the button function o with just one I can send a lot of data ?
admin June 18th, 2009 at 08:09 | #7 Reply | Quote @Lucas
As you see, in c# there is a switch on the command function recived from flash. in the example i gave a comment ago, the function send(message:String) uses the command sendText, but you can use that as a parameter as well.
public function send(command : String, message : String):void
{
ExternalInterface.call(command, message);
}
and in c#
switch (command)
{
case “sendText” : doSometing(); break;
case “Some_Other_Command” : doSometingElse(); break;
case ….
…..
}
Thakur September 22nd, 2009 at 17:30 | #8 Reply | Quote Hi,
This is my first interaction with flex.
Can i use flex to carry out memory dump of flash application without using ShockwaveFlash COM object?
Thanks.
Istvan December 3rd, 2009 at 12:09 | #9 Reply | Quote Hi,
Thank you for this very useful article. Do you have idea, how can I extend this to give back a return value at AS->C# call ?
Because now AS->C# is a simple procedure, not a function call and in a lot case return value is needed…
Scott January 28th, 2010 at 23:59 | #10 Reply | Quote Any way to do this with Flash Lite?
admin January 29th, 2010 at 09:54 | #11 Reply | Quote @Scott
Flash Lite has no equivalent api feature like the AS3 ExternalInterface api. One potential way to send data from Flash to another local application is with fscommand(). Another common approach is to build the native application as a local server and use xml sockets as a way to send data back and forth.
No comments:
Post a Comment