1

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.

8vius
  • 5,786
  • 14
  • 74
  • 136
  • The following [Stack Overflow answer](http://stackoverflow.com/questions/7657842/how-to-call-flash-actionscript-callback-method-from-javascript/7657994#7657994) might contain leads to your specific problem. – Vladimir Tsvetkov Oct 27 '11 at 13:25

1 Answers1

2
  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;
}
Marcelo Assis
  • 5,136
  • 3
  • 33
  • 54
  • yes, this sounds like the problem. before you can call a function from JS on your SWF, the SWF needs to be instantiated AND it's codeblock `ExternalInterface.addCallback` method need to have executed. if you want to call the JS method as soon as it it available, you can fire off a method from Flash to JS as soon as the `addCallback` is executed inside your SWF, since the DOM is open game for Flash to interact with as long as allowScriptAccess is enabled in the Embed – gthmb Oct 27 '11 at 14:26
  • This is the issue, thank you Marcelo, now the deal is this method is to load up the URL to the .mp3 file I want to play (this is an audio player) I tried simply using flashvars, works fine, but If I play one file when I go to play the next it just gets the player from cache and plays the first file I played, hence the ExternalInterface call. So you have an idea, my site has a stream of voicenotes, and whenever I click to play one the player is instantiated for it. – 8vius Oct 27 '11 at 14:30
  • @8vius, why don't explain better your problem? I didn't got it. You need just to have a method to call your songs, and call them via JS, using a param to choose of them. Preferentially, using just one SWF. – Marcelo Assis Oct 27 '11 at 19:42
  • Don't really know how to explain myself better, anyways I solved my cached swf problem by appending to the url of the swf file, just a random string to make it a unique url. Thanks again. – 8vius Oct 27 '11 at 19:58