使用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>
ExternalInterface在Firefox下出错的bug
最新推荐文章于 2015-11-01 19:44:18 发布