Using ExternalInterface in Flash

倖福魔咒の 提交于 2019-11-28 11:35:19

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!");

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/.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!