Click here to Skip to main content
Click here to Skip to main content

Fun with C# and the Flash Player 8 External API

, 19 Oct 2005
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to communicate between Flash Player 8 and C# using the new External API

Introduction

After hearing of all the great new enhancements made to Flash Player 8 during Macromedia MAX this year, I was inspired to take one of the new features that caught my eye for a test drive. The feature I am speaking of is the External API.

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 8) 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!

For my test application, I decided to begin porting over the FLVPlayer I built earlier this year using Flash Player 7, to Flash Player 8. This is just a simple program that allows double click playback of FLV encoded video.

C# -> ActionScript Communication

As I said before, communication between Flash Player 8 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:

import flash.external.ExternalInterface;

Next, we have to register any function we want to make available externally:

ExternalInterface.addCallback("loadAndPlayVideo", null, loadAndPlayVideo);

Basically, the code above will allow us to call the loadAndPlayVideo function (which I will show in a minute) from the C# application. The second argument is null because the loadAndPlayVideo function is declared on the _root of the timeline.

The loadAndPlayVideo function is as below. Basically, it takes a file path and sets it as the content path in an instance of the FLVPlayback component named videoPlayer.

function loadAndPlayVideo(uri:String):Void
{
    videoPlayer.contentPath = uri;
}

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 8 ActiveX control to my form and load the SWF we created in the form's constructor:

private AxShockwaveFlash flashPlayer;

public FLVPlayer ()
{
     // Error handling removed for the sake of condensing the code
     flashPlayer.LoadMovie(0, Application.StartupPath + "\\player.swf");
     .
     .
     .
}

Next, all we have to do is call the externalized method when desired. In my case, it is in response to the user selecting a file from a standard OpenFileDialog:

fileDialog = new OpenFileDialog();
fileDialog.Filter = "*.flv|*.flv";
fileDialog.Title = "Select a Flash Video file...";
fileDialog.Multiselect = false;
fileDialog.RestoreDirectory = true;

if(fileDialog.ShowDialog() == DialogResult.OK)
{
     flashPlayer.CallFunction("<invoke" + 
          " name=\"loadAndPlayVideo\" returntype=\"xml\">
          <arguments><string>" + fileDialog.FileName + 
          "</string></arguments></invoke>"); 
}

That's it! As you can see, we call loadAndPlayVideo by sending in a specially formatted XML string containing the name of the method to invoke and the list of function parameters.

ActionScript -> C# Communication

Again, you will need to include the ExternalInterface in the ActionScript:

import flash.external.ExternalInterface;

Then to make the call from ActionScript we use a static method on the ExternalInterface class:

ExternalInterface.call("ResizePlayer", 
      videoPlayer.metadata.width, videoPlayer.metadata.height);

As you can see, I am calling a method called ResizePlayer and passing the width and height of the video currently loaded as parameters. Now to receive the message in C#, we first have to subscribe to the FlashCall event. To do this, I added the following line to the form's constructor:

flashPlayer.FlashCall += 
  new _IShockwaveFlashEvents_FlashCallEventHandler(flashPlayer_FlashCall);

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:

<invoke name="ResizePlayer" returntype="xml">
     <arguments>
          <number>320</number>
          <number>240</number>
     </arguments>
</invoke>

So now all we have to do is parse the XML in the event handler and invoke the C# function locally:

XmlDocument document = new XmlDocument();
document.LoadXml(e.request);

// Get all the arguments
XmlNodeList list = document.GetElementsByTagName("arguments");

// Since in this case I only have one call from ActionScript to C# I
// know I need to invoke the ResizePlayer function
ResizePlayer(Convert.ToInt32(list[0].FirstChild.InnerText), 
  Convert.ToInt32(list[0].ChildNodes[1].InnerText));

Viola! It is now that easy to pass data back and forth between ActionScript and C#.

What's Next?

As I am sure many of you are thinking, writing and parsing of the XML being sent back and forth can be completely plucked out and wrapped up into utility classes for easy re-use.

As time permits, I will continue to work on porting FLVPlayer into the Flash Player 8 era and will work on building the utility classes necessary. Look for another article with details, when completed…

Example Code

Sample Image - flashexternalapi.jpg

I have made the simple example discussed available. Keep in mind this is only meant to demonstrate the External API and is not the final version of the FLVPlayer. In fact, if you try to load a video larger then 320x240, you will see that the resize functionality is not complete.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Gabe Wishnie
Web Developer
United States United States
.NET Software Developer
 
Education
- Working towards M.S. Computer Science
- B.S. Computer Science
- B.S. Computer Information Systems
- Minor Mathematics
 
Professional
- Microsoft Certified Application Developer
- Microsoft Certified Professional
- C# ASP.NET Developer (4 years)
 
Other Interests
- Flash Remoting
- Video Encoding (Windows Media and Flash Video)
- Microsoft Content Management Server
 
More Programming Thoughts and Tips
- http://gabewishnie.blogspot.com

Comments and Discussions

 
GeneralMy vote of 4 Pinmembermutterholzbein20-Nov-12 5:42 
GeneralMy vote of 4 PinmemberBurak Tunçbilek9-Jul-12 11:02 
GeneralMy vote of 2 Pinmembermannu9t31-Jan-12 4:49 
QuestionSending large data to flash Pinmembervhad0125-Aug-11 20:15 
How about sending large data (eg. byte array data encoded with base64) to flash?
Is it possible?
 
TIA
GeneralMy vote of 5 Pinmembermahtab_y_n20-Jun-11 20:49 
QuestionHow can I know in my C# application when the FLV Video reach the end !!! PinmemberSoit Mircea Ioan8-Apr-11 1:16 
AnswerRe: How can I know in my C# application when the FLV Video reach the end !!! PinmemberMember 460580225-Aug-12 4:15 
GeneralProblems receiving Events Pinmemberfridolin Mueller1-Mar-11 9:52 
QuestionDo you have a new .swf file to show the complete movie? Pinmembervarcario119-Feb-11 18:37 
Generalwindows 64bit Pinmemberducmanh8611-Feb-10 6:56 
GeneralRe: windows 64bit PinmemberMember 112374918-Mar-10 22:16 
GeneralRe: windows 64bit Pinmemberducmanh8610-Aug-10 10:47 
QuestionHow to Stop and pause movie PinmemberMember 383892829-Jan-09 16:55 
GeneralMy vote of 1 PinmemberIkutsin7-Jan-09 1:57 
Questioncan not fine any .flv file to load Pinmembersara saghi23-Dec-08 1:20 
Generalc# and FLASH interfacing Pinmemberjoshi aniruddha7-Jul-08 8:59 
QuestionCOM exception when load FLV file PinmemberMushq8-Jun-08 22:44 
QuestionAbout License PinmemberMushq8-Jun-08 7:48 
Generali got Unspecified error PinmemberHimal Rupera4-Jun-08 0:59 
GeneralRe: i got Unspecified error Pinmemberdroopy610-Sep-11 13:03 
GeneralFunctio LoadVideo(string videoPath) is not working. [modified] PinmemberMember 452199325-May-08 4:36 
GeneralRe: Functio LoadVideo(string videoPath) is not working. PinmemberJuba9-Nov-08 17:18 
Generali want read a file from recources(read byte of file ,not path) Pinmemberalirzn124-May-08 20:54 
GeneralCallFunction request string +XML object Pinmemberkhoffmann7-May-08 4:56 
GeneralHelp me! I can't download source! Pinmemberducmanh8629-Nov-07 7:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 19 Oct 2005
Article Copyright 2005 by Gabe Wishnie
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid