Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

as3 ExternalInterface.addCallback is not working right

I was trying to access swf from javascript, so this example in livedocs is what I'm trying to modify. http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#includeExamplesSummary

However,it is not working correctly for some reason. The problem I'm encountering is that it does not work in Safari and in Firefox, it only works if I put an alert in the function before javascript pass the value to swf. (seems like it needs some time) I also tried to set a timer in as3, but timer doesn't work, only alert in js helps.

All I wanted to do is use js to tell the swf file to play ep1.swf. Here's my js code:

document.observe('dom:loaded', function() {
    $('episode1').observe('click', function() {
        var params = {wmode : "transparent", allowScriptAccess:"always", movie:"header"};
        swfobject.embedSWF("swf/float.swf", "header", "100%", "100%", "9.0.0","expressInstall.swf", "", params, "");
        sendToActionScript("ep1.swf");
    });
})
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        //alert("aaa")
        return document[movieName];
    }
}
function sendToActionScript(value) {     
    thisMovie('header').sendToActionScript(value);       
}

Here's my as3 code:

private function receivedFromJavaScript(value:String):void {

    loader.load(new URLRequest(value));

}

I've been trying for a really long time, does anyone know how to fix this? Thanks.

like image 640
shibbydoo Avatar asked Jan 25 '23 00:01

shibbydoo


2 Answers

The problem is that the SWF file isn't fully loaded by the time you try to call it. The flash player is probably loaded but it takes a while to load and initialise the swf file.

What you need to do is make a call from the SWF file to a javascript function when it's loaded and put your javascript there rather than in the page loaded handler that you seem to be doing now. That way you know that your flash application is properly initialized by then. The ExternalInterface class you are using has methods to let you call back into the javascript.

like image 164
jcoder Avatar answered Jan 26 '23 12:01

jcoder


Use this code to get swf Object.

I tested this code on:

  • IE 9,8,7
  • Firefox 6.0.1
  • Netscape Navigator 9.0.0.6
  • Opera 11.5
  • Google chrome 13.0.782.215
  • Safari 3.2 (All In Windows OS)

and it worked fine.

function GetSWF(strName) {
    if (window.document[strName] != null) {
        if (window.document[strName].length == null)
            return window.document[strName];
        else
            return window.document[strName][1];
    } else {
        if (document[strName].length == null)
            return document[strName];
        else
            return document[strName][1];
    }
}
like image 37
abasan Avatar answered Jan 26 '23 14:01

abasan