为什么我不能让 Javascript 与 ActionScript 对话

发布于 2024-08-20 09:44:42 字数 2060 浏览 4 评论 0原文

我有一个非常简单的 Flash 程序来播放音乐。它由一个播放暂停按钮和一个显示歌曲当前位置的计时器组成。我正在尝试使用常规表单按钮来暂停或播放歌曲。

<div class="musicplayer_playpause">
            <script type="text/javascript">
            AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','65','height','68','src','player','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','wmode','transparent','id','flashobject','movie','player','flashvars','id=<?=$cur_songid;?>&type=<?=$_GET["type"];?>&csid=<?=$cur_songid;?>&l=<?=$Arrcntt+1;?>"' ); //end AC code
            </script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" id="flashobject" width="65" height="68">
              <param name="movie" value="player.swf" allowscriptaccess="always"/>
              <param name="quality" value="high" />
              <param name="wmode" value="transparent" />
        <param name="id" value="flashobject" />
        <param name="swliveconnect" value="true" />
              <embed src="player.swf" name="flashobject" width="65" height="68" quality="high" allowscriptaccess="always" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" wmode="transparent" swliveconnect="true" ></embed>
          </object>
          </noscript></div>

这是我放在以下部分中的 Javascript 函数:

<script type="text/javascript">
function Pause() {
var flash =  document.getElementById('flashobject');
flash.PlayPause;
}
</script>

最后这是我正在使用的按钮:

<form>
<input type="button" value="Play" name="Play" onClick="Pause();"> 
</form>

当我单击该按钮时,Firefox 的错误控制台显示“Flash 为空” 我做错了什么?

I have a very simple flash program that plays music. It consists of a play pause button and a timer that shows the current position of the song. I'm trying to make it possible to pause or play the song using a regular form button.

<div class="musicplayer_playpause">
            <script type="text/javascript">
            AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','65','height','68','src','player','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','wmode','transparent','id','flashobject','movie','player','flashvars','id=<?=$cur_songid;?>&type=<?=$_GET["type"];?>&csid=<?=$cur_songid;?>&l=<?=$Arrcntt+1;?>"' ); //end AC code
            </script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" id="flashobject" width="65" height="68">
              <param name="movie" value="player.swf" allowscriptaccess="always"/>
              <param name="quality" value="high" />
              <param name="wmode" value="transparent" />
        <param name="id" value="flashobject" />
        <param name="swliveconnect" value="true" />
              <embed src="player.swf" name="flashobject" width="65" height="68" quality="high" allowscriptaccess="always" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" wmode="transparent" swliveconnect="true" ></embed>
          </object>
          </noscript></div>

Here's the Javascript function, which I put in the section:

<script type="text/javascript">
function Pause() {
var flash =  document.getElementById('flashobject');
flash.PlayPause;
}
</script>

And finally here's the button I'm using:

<form>
<input type="button" value="Play" name="Play" onClick="Pause();"> 
</form>

When I click the button, Firefox's error console says "Flash is null" What am I doing wrong?

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

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

发布评论

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

评论(4

暮年 2024-08-27 09:44:42

我不认为这可以直接解决您的问题,但我从经验中知道,使用

var flash =  document.getElementById('flashobject');
flash.PlayPause;

不会在所有流行的浏览器中显示相同的行为。您可以尝试使用以下函数返回对象:

function thisMovie(movieName) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
};
thisMovie('flashobject').PlayPause();

但是,如果您使用的是 jQuery,则可以执行以下操作:

$("#flashobject")[0].PlayPause();

编辑:我找到了 thisMovie 函数的更新版本。不过我仍然推荐 jQuery 方式:)

function getFlashMovieObject(movieName){
  if(document.embeds[movieName])
    return document.embeds[movieName];
  if(window.document[movieName])
    return window.document[movieName];
  if(window[movieName])
    return window[movieName];
  if(document[movieName])
    return document[movieName];
  return null;
}

I don't think it's a direct solution to your problem, but I know from experience that using

var flash =  document.getElementById('flashobject');
flash.PlayPause;

will not show the same behaviour in all popular browsers. you can try to use the following function to return the object:

function thisMovie(movieName) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
};
thisMovie('flashobject').PlayPause();

If you are using jQuery however, the following will do:

$("#flashobject")[0].PlayPause();

edit: I found a more updated version of the thisMovie function. I would still recommend the jQuery way though :)

function getFlashMovieObject(movieName){
  if(document.embeds[movieName])
    return document.embeds[movieName];
  if(window.document[movieName])
    return window.document[movieName];
  if(window[movieName])
    return window[movieName];
  if(document[movieName])
    return document[movieName];
  return null;
}
拥有 2024-08-27 09:44:42

包含 Pause 的脚本标记格式错误。向 type 属性添加尾部引号。

此外,AC_FL_RunContent 没有定义电影的 id。添加 4 个参数 - 'id'、'flashobject'、'name'、'flashobject'。

AC_FL_RunContent 在此 Adob​​e 技术说明 http://kb2.adobe.com/cps/127 中进行了描述/tn_12701.html,其中内容如下:

  • id(对象属性,仅限对象)
    电影标识符。向主机环境(a
    例如网络浏览器),以便
    可以使用脚本来引用
    语言。
  • 名称(仅限嵌入)
    电影
    姓名。将 Flash 影片识别为
    主机环境(网络浏览器,
    通常)这样就可以
    使用脚本语言引用
    例如 JavaScript 或 VBScript。

The script tag containing Pause is malformed. Add a trailing quote to the type attribute.

Additionally, AC_FL_RunContent is not defining an id for the movie. Add 4 more params to it - 'id', 'flashobject', 'name', 'flashobject'.

AC_FL_RunContent is described in this Adobe technote http://kb2.adobe.com/cps/127/tn_12701.html, which says this:

  • id (attribute for object, object only)
    Movie Identifier. Identifies the Flash movie to the host environment (a
    web browser, for example) so that it
    can be referenced using a scripting
    language.
  • name (embed only)
    Movie
    name. Identifies the Flash movie to
    the host environment (a web browser,
    typically) so that it can be
    referenced using a scripting language
    such as JavaScript or VBScript.
匿名。 2024-08-27 09:44:42

找到这个:
替换为

 <param name="movie" value="player.swf" />
   <param name="allowScriptAccess" value="always" />

我仍然会使用 Jasper 的函数

find this: <param name="movie" value="player.swf" allowscriptaccess="always"/>
replace with

 <param name="movie" value="player.swf" />
   <param name="allowScriptAccess" value="always" />

I would still use Jasper's function

泪冰清 2024-08-27 09:44:42

Jasper 具有正确的 JavaScript 方法,但您还需要 在 ActionScript 中为 JS 函数添加回调,以便 AS 知道监听它们。

AS代码:

import flash.external.ExternalInterface;
ExternalInterface.addCallback("PlayPause", playPauseMovie);
...
function playPauseMovie():void {
   //code goes here
}

Jasper has the right JavaScript method, but you also need to add callbacks for your JS functions in ActionScript, so that AS knows to listen for them.

AS code:

import flash.external.ExternalInterface;
ExternalInterface.addCallback("PlayPause", playPauseMovie);
...
function playPauseMovie():void {
   //code goes here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文