C# / ActionScript with AxShockwaveFlash
Asked Answered
C

1

2

I am new to ActionScript and am trying to communicate between AS and C#. Every tutorial/explanation I find seems to be exactly the same, but I simply cannot get it to work. callFunction throws a COM exception ("E_FAIL"), and when I try calling ExternalInterface.call() from the AS, it never seems to.

I've gotten this to work in JavaScript/HTML, but I'm out of ideas for C#; I suspect I'm doing something wrong/not allowed in my AS. I compile the AS file with Adobe Flex 4.6 and mxmlc.

Edit: Just to clarify, this code shows only the test with calling a C# function from AS since it seems less error-prone (no xml argument handling needed). Also, I've run a test with a while(true) ExternalInterface.call("someFct", "Hello world"); loop in my AS and my CPU usage for the debug process is basically the same as with no instructions in the AS (~0.3%). So it seems the code doesn't execute at all.

Here is my AS file:

package
{
    import flash.external.ExternalInterface;
    public class Test
    {     
        public function Test()
        {
            ExternalInterface.call("someFct", "Hello world");
        }
    }
}

And in C# I have an AxShockwaveFlash control in WinForms:

private void InitializeComponent()
{
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
    this.axShockwaveFlash1 = new AxShockwaveFlashObjects.AxShockwaveFlash();
    ((System.ComponentModel.ISupportInitialize)(this.axShockwaveFlash1)).BeginInit();
    this.SuspendLayout();
    // 
    // axShockwaveFlash1
    // 
    this.axShockwaveFlash1.Enabled = true;
    this.axShockwaveFlash1.Location = new System.Drawing.Point(104, 67);
    this.axShockwaveFlash1.Name = "axShockwaveFlash1";
    this.axShockwaveFlash1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axShockwaveFlash1.OcxState")));
    this.axShockwaveFlash1.Size = new System.Drawing.Size(574, 314);
    this.axShockwaveFlash1.TabIndex = 0;
    this.axShockwaveFlash1.FlashCall += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEventHandler(this.recv);
    this.Click += new System.EventHandler(this.Form1_Click);
//...
    ((System.ComponentModel.ISupportInitialize)(this.axShockwaveFlash1)).EndInit();
     this.ResumeLayout(false);
}

Initializing the SWF object and receiving its call:

private void Form1_Click(object sender, EventArgs e)
{
    axShockwaveFlash1.LoadMovie(0, @"mypath\test.swf");
}

//Never executes
public void recv(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
{
    string s = e.request;
}
Centralization answered 23/11, 2015 at 6:14 Comment(0)
C
3

I found the problems.

1) As I suspected, the COM object needs some kind of graphic initialization in order to work at all. Making your main class extend Sprite solves this.

2) You apparently can't use ExternalInterface.call() or ExternalInterface.setCallback() in the main class' constructor directly. Creating an instance of a new class in the main class' constructor and making the ExternalInterface call in its constructor or any other function solves this.

Test.as:

package
{
    import flash.display.*;
    public class Test extends Sprite
    {     
        public function Test()
        {
            super();
            var x : Test1 = new Test1();
        }
    }
}

And Test1.as:

package
{   
    import flash.external.ExternalInterface;
    public class Test1
    {
        public function Test1()
        {
            ExternalInterface.call("recv", "Hello world");
        }
    }
}

I also have no idea why someone would downvote my question...

Centralization answered 24/11, 2015 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.