javascript 如何克服“调用 NPObject 方法时出错”和 Play() SWF?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5607726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:45:26  来源:igfitidea点击:

How can I overcome "Error calling method on NPObject" and Play() a SWF?

javascriptjqueryflashswfobject

提问by Kirkman14

I am working on a quiz system. Normally it uses jpeg images with each question. But a few of the quizzes instead feature animated .SWF files. I want to use jquery swfobject to call the play() method, once the user has clicked on an answer.

我正在研究测验系统。通常它对每个问题使用 jpeg 图像。但是一些测验反而以动画 .SWF 文件为特色。一旦用户单击了答案,我想使用 jquery swfobject 调用 play() 方法。

My solution works fine locally, but when I try to run it online, it throws the "Error calling method on NPObject!" message.

我的解决方案在本地运行良好,但是当我尝试在线运行它时,它抛出“错误调用 NPObject 方法!” 信息。

The quiz HTML is located on www.somedomain.com and the SWFs are on images.somedomain.com. So, I'm guessing this is a cross-domain thing.

测验 HTML 位于 www.somedomain.com,SWF 位于 images.somedomain.com。所以,我猜这是一个跨域的事情。

Trouble for me is, I don't know anything about Flash... how to edit SWF files, anything about Flash attributes, or the like.

我的问题是,我对 Flash 一无所知...如何编辑 SWF 文件、有关 Flash 属性的任何信息,等等。

Here's an example of my code:

这是我的代码示例:

<div class="photos">
    <object data="http://images.somedomain.com/q1tattooquiz.swf" type="application/x-shockwave-flash" width="600" height="350">
        <param name="movie" value="http://images.somedomain.com/q1tattooquiz.swf">
        <param name="wmode" value="opaque">
    </object>
</div>

And the javascript:

和 javascript:

if (hasFlash) {
    $('#question'+quesNum+' .photos').flash(function() { this.Play(); } );
    }

It's pretty simple, and as I said, it works fine on my PC. But once uploaded, it errors out.

这非常简单,正如我所说,它在我的 PC 上运行良好。但是一旦上传,它就会出错。

What can I add to my javascript to make these files work right?

我可以向我的 javascript 添加什么来使这些文件正常工作?

回答by user776686

Kirkman14,

柯克曼14,

Apart from the allowScriptAccess thingy, make sure you are calling the functions with the correct number of parameters, matching AS declaration. If your AS declares:

除了 allowScriptAccess 之外,请确保使用正确数量的参数调用函数,匹配 AS 声明。如果您的 AS 声明:

function playSound(event:MouseEvent):void{
// code here
}

then make sure you use the function accordingly playSound(null). I've been through this myself.

然后确保相应地使用该功能playSound(null)。我自己也经历过。

回答by pixelfreak

I think there isn't a way to avoid changing the SWF. You need to put this line in Flash: flash.System.Security.allowDomain('*');This is only needed it you are trying to call a function in the SWF from JS. For the opposite direction, also set allowScriptAccessto alwaysin your embed code. Note that this param might be case-sensitive, if I remember correctly.

我认为没有办法避免更改 SWF。您需要将此行放在 Flash 中:flash.System.Security.allowDomain('*');这仅在您尝试从 JS 调用 SWF 中的函数时才需要。对于相反的方向,还将allowScriptAccess设置为始终在您的嵌入代码中。请注意,如果我没记错的话,这个参数可能区分大小写。

回答by Lars Bl?sj?

You could try setting allowscriptaccess to "always". allowscriptaccess is the same kind of parameter as wmode in your sample code.

您可以尝试将 allowscriptaccess 设置为“始终”。allowscriptaccess 与示例代码中的 wmode 参数类型相同。

回答by B T

It was suggested to me to create crossdomain.xml files that whitelist domains the flash will communicate with. I haven't had any success with this yet though (but perhaps my problem is different from yours).

有人建议我创建 crossdomain.xml 文件,将 Flash 将与之通信的域列入白名单。尽管如此,我还没有取得任何成功(但也许我的问题与你的不同)。

回答by Amalgovinus

For me, this issue arose because, in Flex, I'd set to static a function that bore ExternalInterface calls. If the problem function is static, try undoing that.

对我来说,出现这个问题是因为在 Flex 中,我将一个包含 ExternalInterface 调用的函数设置为静态。如果问题函数是静态的,请尝试撤消它。