Using ExternalInterface in Flash
Asked Answered
H

2

4

I'm trying to edit some flash to make an external javascript function call, but with no success. Here's my actionscript 2.0 code:

//testing external .js calls

import flash.external.ExternalInterface;

//attempting to make external js call

ExternalInterface.call("createPlaylist","It's my Life!");

and here's my javascript;

function createPlaylist(mess){
  alert("called createPlaylist: " + mess);
}

I've seen lots of examples and I'm mainly confused about the use of ExternalInterface.addCallback. I don't need the javascript to return anything to flash, so is this necessary?

For whatever reason, I never see the alert. Does anyone see any problems in my code? Is there some ExternalInterface library I don't have? Also, what's the BEST way to use ExternalInterface (ie; error checking, etc.) Thanks in advance...

Hallucinosis answered 3/5, 2009 at 21:48 Comment(0)
P
7

ExternalInterface.addCallback is for javascript to be able to call into your Flash application. If for example you want a HTML button that starts/stops a video you just add a callback for a named method and your js can than call [FlashObject].callback method name.

I would say that the best way to add ExternalInterface methods in your application is to set up a class responsible for JS communication for each interaction case in the app. For example:

public class ExternalVideoControl {

    private var video:MediaDisplay;

    public function ExternalVideoControl(video:MediaDisplay) {
        //ExternalInterface.addCallback  - one callback for each method you want to expose, pointing to a method within this class;
        //add listeners on the video player and point them to methods in this class, for example onProgress
    }
    public function playVideo():void {
        //play the video on the mediaDisplay
    }
    private function onProgress(event:ProgressEvent):void {
        //ExternalInterface.call - report progress back to javascript
    }
}

To test ExternalInterface more directly, try calling

ExternalInterface.call("alert", "Hello World!");
Pearlypearman answered 4/5, 2009 at 10:20 Comment(11)
Thanks, that what I thought the callback method was for. Any ideas as to why I'm not seeing the alert (in my code)?Hallucinosis
Try tracing the ExternalInterface.available property. It tells you if the flash is running in a container allowing ExtInt calls. Read more @ livedocs.adobe.com/flex/2/langref/flash/external/…Varus
with something like this? //testing external .js calls import flash.external.ExternalInterface; //is external interface available? var isAvailable:Boolean = ExternalInterface.available; trace(isAvailable); What should I be looking for when I run this?Hallucinosis
Sorry, can't figure out how to post code. Can anyone read that?Hallucinosis
You should look for wether isAvailable is true or not. If not - that's your problem.Varus
ExternalInterface.available is true if you can run ExternalInterface. Just look in the trace if you are running in debug mode.Varus
That's what I'm asking about, 'trace'. I looked up the adobe docs on trace and still don't really understand. Will Firebug show me the trace? Testing now...Hallucinosis
traces show up in the Flash or Flex IDE. You can view traces in Firefox using FlashTracer addons.mozilla.org/firefox/addon/3469Varus
Assuming what is in a container? I don't understand what you mean.Varus
This goes in the wrong direction. The poster wants to call Java Script from Action Script. Add Callback allows JavaScript to call ActionScript functions.Pule
"Try tracing the ExternalInterface.available property. It tells you if the flash is running in a container allowing ExtInt calls." Is the 'container' I was asking about...Hallucinosis
P
0

As others said in the comments to the post of Johan, you should first check, if the External Interface is available by checking ExternalInterface.available.

Other than that... how do you launch the Flex Application? First it must be included in a wrapper that contains this JavaScript of course. That's trivial. However in case you launch it as file from the local filesystem (browser URL starts with file://) then you must also make sure that the SWF file has the required permissions to run a JavaScript function.

You must trust the SWF file to make it able to access local resources like files, or JS on local files. To do that create a file like myapp.cfg and add the path to your file as a single line to this line. Place this file in the FlashPLayerTrust folder. On Linux systems this is ~/.macromedia/Flash_Player/#Security/FlashPlayerTrust/.

Pule answered 8/5, 2009 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.