Search code examples
javascriptjqueryflashactionscript-3externalinterface

ExternalInterface callback not working


I'm trying to call a function in my ActionScript from my JavaScript, I've managed to successfully do this with another flash file but in the current one it's failing for some reason. I'm using jQuery SWFEmbed, this is my JS code:

$.ajax({
        url: '<?php echo $html->url(array('controller' => 'voicenotes', 'action' => 'get_url'), true);?>' + '/' + container.children('#VoicenoteVnid').val(),
        dataType: 'json',
        type: 'POST',
        success: function(response) {
          container.children('.voicenote-info').children('.player').addClass('active');
          flashMovie = container.children('.voicenote-info').children('.player');
          alert(flashMovie.html());
          flashMovie.flash({
            swf: '<?php echo $html->url('/files/flash/reproductor_compact.swf',true); ?>',
            width: 240,
            height: 40,
            quality: "high",
            wmode: "transparent",
            allowscriptaccess: "always",
          });
          alert($('.player.active > object')[0].callIt());
        }              
      });

And here is my AS code, this is in my constructor:

public function reproductor()
        {
            ExternalInterface.addCallback("callIt", test);
            ExternalInterface.call("alert", "Que fue?");
            trace(ExternalInterface.available);
        }

And this is my function:

private function test():String {
            return "this is a test";
        }

The ExternalInterface.call work, and the trace outputs true, I have no idea what's going on.

P.S: If you could also tell me how I can pass parameters to a ExternalInterface callback I'd appreciate it.


Solution

    1. Maybe your Jquery selector to find your SWF object is wrong. Why don't you try to do it another way, just for debugging?
    2. Maybe you are calling your AS3 function before the SWF has fully loaded. Why don't you try place that function call(callIt) into an button, outsite that Ajax, and press it after your alert have been called?

    To receive a param within your callback function, just send it by JS, and receive it as an argument in your callback function. Ex.:

    $('.player.active > object')[0].callIt("LOLSOME")
    
    ...
    
    ExternalInterface.addCallback("callIt", test);
    private function test(arg:String):String {
            return "param received from JS: " + arg;
    }