[AS3]Flash Player 与 HTML 容器之间发送数据的过程
以下示例演示了在 Flash Player 与 HTML 容器之间发送数据的过程。
以下示例演示了在 Flash Player 与 HTML 容器之间发送数据的过程。
- package {
- import flash.display.Sprite;
- import flash.events.*;
- import flash.external.ExternalInterface;
- import flash.text.TextField;
- import flash.utils.Timer;
- import flash.text.TextFieldType;
- import flash.text.TextFieldAutoSize;
- public class ExternalInterfaceExample extends Sprite {
- private var input:TextField;
- private var output:TextField;
- private var sendBtn:Sprite;
- public function ExternalInterfaceExample() {
- input = new TextField();
- input.type = TextFieldType.INPUT;
- input.background = true;
- input.border = true;
- input.width = 350;
- input.height = 18;
- addChild(input);
- sendBtn = new Sprite();
- sendBtn.mouseEnabled = true;
- sendBtn.x = input.width + 10;
- sendBtn.graphics.beginFill(0xCCCCCC);
- sendBtn.graphics.drawRoundRect(0, 0, 80, 18, 10, 10);
- sendBtn.graphics.endFill();
- sendBtn.addEventListener(MouseEvent.CLICK, clickHandler);
- addChild(sendBtn);
- output = new TextField();
- output.y = 25;
- output.width = 450;
- output.height = 325;
- output.multiline = true;
- output.wordWrap = true;
- output.border = true;
- output.text = "Initializing...\n";
- addChild(output);
- if (ExternalInterface.available) {
- try {
- output.appendText("Adding callback...\n");
- ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
- if (checkJavaScriptReady()) {
- output.appendText("JavaScript is ready.\n");
- } else {
- output.appendText("JavaScript is not ready, creating timer.\n");
- var readyTimer:Timer = new Timer(100, 0);
- readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
- readyTimer.start();
- }
- } catch (error:SecurityError) {
- output.appendText("A SecurityError occurred: " + error.message + "\n");
- } catch (error:Error) {
- output.appendText("An Error occurred: " + error.message + "\n");
- }
- } else {
- output.appendText("External interface is not available for this container.");
- }
- }
- private function receivedFromJavaScript(value:String):void {
- output.appendText("JavaScript says: " + value + "\n");
- }
- private function checkJavaScriptReady():Boolean {
- var isReady:Boolean = ExternalInterface.call("isReady");
- return isReady;
- }
- private function timerHandler(event:TimerEvent):void {
- output.appendText("Checking JavaScript status...\n");
- var isReady:Boolean = checkJavaScriptReady();
- if (isReady) {
- output.appendText("JavaScript is ready.\n");
- Timer(event.target).stop();
- }
- }
- private function clickHandler(event:MouseEvent):void {
- if (ExternalInterface.available) {
- ExternalInterface.call("sendToJavaScript", input.text);
- }
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...