如何从swfobject中检测Flash事件

时间:2010-09-28 20:30:07

标签: jquery flash

我一直在阅读使用ExternalInterface让Flash与JavaScript通信。当用户开始播放视频和视频结束时,我需要在JavaScript中检测并调用一些代码。 swfobject非常适合在页面上嵌入播放器,但我无法弄清楚如何将这些事件处理程序添加到它。这是我的代码:

function flashFallback(){
jQuery('#dialog').dialog({
  modal:true,
  autoOpen:false,
  height: 119
});
jQuery("#dialog").dialog("open");

setTimeout(function(){
  var el = "";
  var vidFileName = "";
  var posterPath = "";
  var videoTag = "";
  var boxId = "";
  var so = "";
  var flashId = "";
  var flashVars = "";
  var params = "";
  var attributes = "";
  var anchorId = "";
  var dotPosition = "";
  var count = jQuery("video").length;
  //alert(count);

  jQuery("video").each(function(){
    el = "";
    vidFileName = "";
    posterPath = "";
    videoTag = "";
    flashId = "";
    flashVars = "";
    params = "";
    attributes = "";
    anchorId = "";

    el = jQuery(this);

    boxId = this.id + "_flashBox";
    flashId = this.id + "_flashPlayer";
    anchorId = this.id + "_anchor";

    el.parent().parent().find("div:first").attr("id",boxId);

    el.parent().find("source").each(function(){
      if (jQuery(this).attr("src").indexOf("m4v") != -1 || 
        jQuery(this).attr("src").indexOf("mp4") != -1){
        vidFileName = jQuery(this).attr("src").substring(jQuery(this).attr("src").lastIndexOf("/")+1);
      }
    });

    /*
      IE uses the Flash player, which overlays a 'Play' button
      on the poster image by default; so we use a poster image
      that doesn't have a play button. Otherwise we'd end up 
      with a play button on top of a play button.
    */

    dotPosition = el.parent().find("img").attr("src").lastIndexOf(".");
    posterPath = el.parent().find("img").attr("src").substring(0,dotPosition) + "-ie" + el.parent().find("img").attr("src").substring(dotPosition);

    el = jQuery("[id="+boxId+"]");
    el.empty();
    el.append("<a id='" + anchorId +"'></a>");

    flashvars =
    {
      file:                 vidFileName,
      autostart:            'false',
      image:                  posterPath
    };

    params =
    {
      allowfullscreen:      'true', 
      allowscriptaccess:    'always',
      wmode:                  'opaque'

    };

    attributes =
    {
      id:                   flashId, 
      name:                 flashId
    };

    swfobject.embedSWF('global/vid/player.swf', anchorId, '372', '233', '9.0.124', 'global/js/swfobject/expressInstall.swf', flashvars, params, attributes);
  });
  jQuery('#dialog').dialog("close");
}, 200);

}

1 个答案:

答案 0 :(得分:1)

你不必为此烦恼。只需检测flash中的事件,然后在flash事件处理程序中调用所需的javascript函数:

import flash.external.ExternalInterface;
...
ExternalInterface.call("your_javascript_function()");

http://codingrecipes.com/calling-a-javascript-function-from-actionscript-3-flash