在 Flash 中使用外部接口

发布于 2024-07-18 06:42:46 字数 677 浏览 7 评论 0原文

我正在尝试编辑一些 Flash 以进行外部 javascript 函数调用,但没有成功。 这是我的 actionscript 2.0 代码:

//testing external .js calls

import flash.external.ExternalInterface;

//attempting to make external js call

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

这是我的 javascript;

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

我看过很多例子,我主要对 ExternalInterface.addCallback 的使用感到困惑。 我不需要 javascript 将任何内容返回到 flash,所以这是必要的吗?

无论出于何种原因,我从未看到该警报。 有人看到我的代码有什么问题吗? 是否有一些我没有的 ExternalInterface 库? 另外,使用 ExternalInterface 的最佳方式是什么(即;错误检查等)。提前致谢...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你げ笑在眉眼 2024-07-25 06:42:46

ExternalInterface.addCallback 是为了让 javascript 能够调用您的 Flash 应用程序。 例如,如果您想要一个开始/停止视频的 HTML 按钮,您只需为命名方法添加回调,然后您的 js 就可以调用 [FlashObject].callback 方法名称。

我想说,在应用程序中添加ExternalInterface方法的最佳方法是设置一个类,负责应用程序中每个交互案例的JS通信。 例如:

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
    }
}

要更直接地测试ExternalInterface,请尝试调用

ExternalInterface.call("alert", "Hello World!");

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!");
秋风の叶未落 2024-07-25 06:42:46

正如其他人在 Johan 帖子的评论中所说,您应该首先通过检查ExternalInterface.available 来检查外部接口是否可用。

除此之外...如何启动 Flex 应用程序? 当然,首先它必须包含在包含此 JavaScript 的包装器中。 那是微不足道的。 但是,如果您从本地文件系统(浏览器 URL 以 file:// 开头)将其作为文件启动,则还必须确保 SWF 文件具有运行 JavaScript 函数所需的权限。

您必须信任 SWF 文件,使其能够访问本地资源(例如文件)或本地文件上的 JS。 为此,创建一个类似 myapp.cfg 的文件,并将文件的路径作为一行添加到该行。 将此文件放入 FlashPLayerTrust 文件夹中。 在 Linux 系统上,这是 ~/.macromedia/Flash_Player/#Security/FlashPlayerTrust/。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文