Saturday, June 30, 2007

How to Play Flash (SWF) inside C#

Playing Flash (*.SWF) inside C# program with the ability to play ,pause and forward the movie :
All what you should to do in the first is to import the this two reference in the project as in this figure .
In the InitializeComponent function from Form.Designer.cs define your flash object as this :

this.AxShockwaveFlash1 = new AxShockwaveFlashObjects.AxShockwaveFlash();
then
//
// AxShockwaveFlash1
//
this.AxShockwaveFlash1.Enabled = true;
this.AxShockwaveFlash1.Location = new System.Drawing.Point(12, 12);
this.AxShockwaveFlash1.Name = "AxShockwaveFlash1";
this.AxShockwaveFlash1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("AxShockwaveFlash1.OcxState")));
this.AxShockwaveFlash1.Size = new System.Drawing.Size(525, 266);

To fill your Flash Object by SWF file:
AxShockwaveFlash1.Movie = “c:\ahmed.swf”;

To forward or pause the flash object :
AxShockwaveFlash1.Forward();

To Play the flash object :
AxShockwaveFlash1.Play();

21 comments:

  1. Nice post, Essawy
    You may got this error
    Cannot initilize the component...

    So Start->Run->

    regsvr32 C:\Windows\system32\Macromed\Flash\flash7.ocx
    OR
    regsvr32 C:\Windows\system32\Macromed\Flash\flash8.ocx
    OR
    regsvr32 C:\Windows\system32\Macromed\Flash\flash9c.ocx

    Just see your OCX version.

    ReplyDelete
  2. Anonymous7:58 AM

    The download link is broken.
    I also found this link useful http://blogs.vbcity.com/drydo/archive/2005/12/07/5731.aspx

    ReplyDelete
  3. Hello, where do you get those two dlls from. Thank You.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Anonymous2:34 AM

    what is this resource??
    resources.GetObject....
    it gives me error

    ReplyDelete
  6. Hi

    This code is really good,but i am getting error at resource.getobject()... what is this resources. Please give reply

    Bye

    ReplyDelete
  7. Dear Hossam and lavanya :

    1- here is the resource file
    http://ahmedessawy.googlepages.com/Form1.resx

    2-I lost my project but here a VB.net code use the same dlls
    http://ahmedessawy.googlepages.com/Flash_player.rar

    ReplyDelete
  8. alternative solution is:
    first add the flash object in tool box by right click -> com object-> flash object
    then try this
    private void button1_Click(object sender, EventArgs e)
    {
    openFileDialog1.Filter = "swf File|*.swf";
    openFileDialog1.FilterIndex = 2;
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    axShockwaveFlash1.Stop();
    axShockwaveFlash1.Movie = openFileDialog1.FileName;
    axShockwaveFlash1.Play();
    }
    }

    ReplyDelete
  9. Hi, can you please tell me if it is possible to display a transparent swf in flash player in a WPF application. Any feedback would be greatly appreciated.

    ReplyDelete
  10. @myncs
    yes, it is possible to open SWF Flash in your WPF application but keep in mind that
    #Adobe does not officially support embedding the Macromedia Flash Player ActiveX control within a Windows application. This means that future versions of the player may not work with your application.

    #It requires users to install the Adobe Flash Player ActiveX control on their computers. This can be done by installing the player within Microsoft Internet Explorer. Although it is technically possible to distribute the player, that is restricted by the player's end-user license agreement. If you need to distribute the Adobe Flash Player ActiveX control, contact Adobe for permission.

    here is a great post by Janiv Ratso
    to do that step by step and source code is also available :)

    http://blogs.microsoft.co.il/blogs/janiv/archive/2009/09/20/embedding-and-communicating-with-the-macromedia-flash-player-in-wpf.aspx

    ReplyDelete
  11. I have embedded flash control to my C# app and am able to load swf files dynamically. I would like to send information to the swf (for example, my swf file picks data from an external text file) i want to be able to give the name of the text file to the swf file. how can i do this?

    ReplyDelete
  12. @Muthu I didn't try that before but it is applicable and can be done very easy.
    you can google for that use key words like
    "how to pass parameter from C# to flash swf".

    and here two result that I think may help you :)

    http://www.shinedraw.com/browser-integration/silverlight-vs-flash-passing-query-string-parameters/

    http://www.friendsofed.com/samples/1590595173.pdf

    ReplyDelete
  13. Anonymous8:29 PM

    I try to add flash object in vs2008
    it gives a exception
    in designer.cs


    Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

    if anyone know a solution Please reply

    ReplyDelete
  14. Anonymous8:53 PM

    Re: Class not registered (Exception from HRESULT:

    you must change project property - bulid - platworm x86 in other words ocx won't work when x64 or any cpu is selected

    ReplyDelete
  15. Anonymous2:30 PM

    is this possible in c# 2005 ?

    ReplyDelete
  16. yes just write the same code in VS 2005. :)

    ReplyDelete
  17. Essawy, The DLL download link is broken. Will you provide another link or explain further in how I may be able to obtain the Interop.ShockwaveFlashObjects.dll and AxInterop.ShockwaveFlashObjects.dll.

    Thanks...

    ReplyDelete
  18. Dear pconnoll,
    try this link :)
    http://ahmedessawy.googlepages.com/Flash_player.rar

    ReplyDelete
  19. Essawy,

    Thank you for the updated link.

    -Paul

    ReplyDelete
  20. Anonymous7:35 AM

    How to play exe flash files ?

    ReplyDelete