ExternalInterface在Firefox下出错的bug

使用Document Class为避免命名空间冲突,通常会取消Automatically dedare stage instances(自动声明舞台实例)设置。这样会导致一个诡异的问题:Firefox下的ExternalInterface.callback会失 效。另外,使用flash.external.ExternalInterface全名而不是直接使用ExternalInterface,也会避免 Firefox各版本下的一部分奇怪的问题。

下面是一个刚写的一个ExternalInterface的扩展类,用于判断ExternalInterface是否可用。

view plainprint?
package cn.myspace.actionscript.utils  
{  
  import flash.events.Event;  
  import flash.events.EventDispatcher;  
  import flash.events.IEventDispatcher;  
  import flash.events.TimerEvent;  
  import flash.external.ExternalInterface;  
  import flash.utils.Timer;  
  
  public class ExternalInterfaceUtil extends EventDispatcher  
  {  
    //  
    //ExternalInterface Setting  
    public static const EXTERNAL_ISREADY:String = "isReady";  
    public static const EXTERNAL_DEBUG:String = "displayDebug";  
      
    //  
    //Event type  
    public static const EXTERNAL_READY:String = "externalReady";  
    public static const EXTERNAL_OUT_OF_TIME:String = "externalOutOfTime";  
      
    //  
    //class setting  
    private const MAX_COUNT:uint = 30;  
      
    private var readyTimeCounter:int = 0;  
          
    public function ExternalInterfaceUtil(target:IEventDispatcher=null)  
    {  
      super(target);  
      this.registerExternalInterface();  
    }  
      
    //  
    //private  
          
    private function registerExternalInterface():void  
    {        
      if(flash.external.ExternalInterface.available)  
      {  
        try  
        {            
          if(this.isContainerReady)  
          {  
            setupCallBacks();  
          }  
          else  
          {  
            var readyTimer:Timer=new Timer(100);  
            readyTimer.addEventListener(TimerEvent.TIMER,onTimer_handler);  
            readyTimer.start();  
          }  
        }  
        catch(e:Error)  
        {  
            
        }  
      }  
        
    }  
      
    private function get isContainerReady():Boolean  
    {  
       var result:Boolean=Boolean(flash.external.ExternalInterface.call(ExternalInterfaceUtil.EXTERNAL_ISREADY));  
       return result;  
     }  
       
     private function onTimer_handler(e:TimerEvent):void  
     {  
       if(this.isContainerReady)  
       {  
         Timer(e.target).stop();  
         setupCallBacks();  
       }  
       else  
       {  
         if(this.readyTimeCounter < this.MAX_COUNT)  
           this.readyTimeCounter++;  
         else  
         {  
           Timer(e.target).stop();    
           this.dispatchEvent(new Event(ExternalInterfaceUtil.EXTERNAL_OUT_OF_TIME));  
         }  
             
       }  
     }  
       
     private function setupCallBacks():void  
     {  
       this.dispatchEvent(new Event(ExternalInterfaceUtil.EXTERNAL_READY));  
     }  
  }  
}  
package cn.myspace.actionscript.utils {   import flash.events.Event;   import flash.events.EventDispatcher;   import flash.events.IEventDispatcher;   import flash.events.TimerEvent;   import flash.external.ExternalInterface;   import flash.utils.Timer;    public class ExternalInterfaceUtil extends EventDispatcher   {     //     //ExternalInterface Setting     public static const EXTERNAL_ISREADY:String = "isReady";     public static const EXTERNAL_DEBUG:String = "displayDebug";          //     //Event type     public static const EXTERNAL_READY:String = "externalReady";     public static const EXTERNAL_OUT_OF_TIME:String = "externalOutOfTime";          //     //class setting     private const MAX_COUNT:uint = 30;          private var readyTimeCounter:int = 0;              public function ExternalInterfaceUtil(target:IEventDispatcher=null)     {       super(target);       this.registerExternalInterface();     }          //     //private              private function registerExternalInterface():void     {             if(flash.external.ExternalInterface.available)       {         try         {                     if(this.isContainerReady)           {             setupCallBacks();           }           else           {             var readyTimer:Timer=new Timer(100);             readyTimer.addEventListener(TimerEvent.TIMER,onTimer_handler);             readyTimer.start();           }         }         catch(e:Error)         {                    }       }            }          private function get isContainerReady():Boolean     {        var result:Boolean=Boolean(flash.external.ExternalInterface.call(ExternalInterfaceUtil.EXTERNAL_ISREADY));        return result;      }            private function onTimer_handler(e:TimerEvent):void      {        if(this.isContainerReady)        {          Timer(e.target).stop();          setupCallBacks();        }        else        {          if(this.readyTimeCounter < this.MAX_COUNT)            this.readyTimeCounter++;          else          {            Timer(e.target).stop();              this.dispatchEvent(new Event(ExternalInterfaceUtil.EXTERNAL_OUT_OF_TIME));          }                    }      }            private function setupCallBacks():void      {        this.dispatchEvent(new Event(ExternalInterfaceUtil.EXTERNAL_READY));      }   } }

使用:


private function initExternalInterfaceUtil ():void
{
  var ext:ExternalInterfaceUtil = new ExternalInterfaceUtil();
  ext.addEventListener(ExternalInterfaceUtil.EXTERNAL_READY, onReady_handler);
}

private function onReady_handler(e:Event):void
{
  //flash.external.ExternalInterface.addCallback(this.AS_DEBUG, displayDebugInformation);
  //flash.external.ExternalInterface.addCallback(this.AS_PLAY, startPlay);
}


在页面上,添加如下JS:

<script>
var jsReady = false;
var flashMovie;
function isReady()
{
  return jsReady;
}
function init()
{
  jsReady = true;
  if (document.getElementById)
  {
    flashMovie = document.getElementById("flashObject");
  }

}

window.onload = init;

function callExternalInterface()
{
  if (flashMovie)
  {
    flashMovie.startPlay();
  }
}
</script>

<div id="flashcontent"></div>

<script type="text/javascript">
  var fo = new SWFObject("flashEXTest.swf", "flashObject", "300", "350", "8.0.15", "#ffffff", true);
  fo.addParam("wmode", "opaque");
  fo.addParam("allowScriptAccess", "always");
  fo.write("flashcontent");
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值