[ActionScript] AS3利用SWFObject與JS通訊

首先介紹SWFObject的用法:javascript

swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes) 5個必須的參數和4個可選的參數:java

1. swfUrlString,必須的)指定SWFURLexpress

2. idString,必須的)指定將會被Flash內容替換的HTML元素(包含你的替換內容)的idide

3. widthString,必須的)指定SWF的寬。this

4. heightString,必須的)指定SWF的高。url

5. versionString,必須的)指定你發佈的SWF對應的Flash Player版本(格式爲:major.minor.releasespa

6. expressInstallSwfurlString,可選的)指定express install SWFURL並激活Adobe express install debug

7. flashvarsString,可選的)用name:value對指定你的flashvarscode

8. paramsString,可選的)用name:value對指定你的嵌套object元素的paramsorm

9. attributesString,可選的)用name:value對指定object的屬性

 

如下這段代碼給出了SWFObject的經常使用參數:

var so = new SWFObject(swf, id, width, height, version, backgroundColor ,[quality, xiRedirectUrl, redirectUrl, detectKey]);

so.addParam("Param1", "Param2");

so.addParam("Param3", "Param4");

so.addParam("Param5", "Param6");

so.addVariable("variable1", "value1");

so.addVariable("variable2", "value2");

so.addVariable("variable3", "value3");

so.addVariable("variable1", getQueryParamValue("variable1"));

so.addVariable("variable2", getQueryParamValue("variable2"));   so.write("content");

解釋一下這些參數的做用:

var so = new SWFObject(swf, id, width, height, version, backgroundColor ,[uality, xiRedirectUrl, redirectUrl, detectKey]);         //這段是SWFObject必須的基本參數,全部SWFObject都必須包含的。  

 so.addParam("Param1", "Param2");   //這裏是給Flash添加內聯參數,能夠實現諸如背景透明之類的效果。依照需求添加就能夠了,參數和emded/object標籤是同樣的代碼,下同。   

so.addParam("Param3", "Param4");

so.addParam("Param5", "Param6");

so.addVariable("variable1", "value1");    //這裏是給Flash添加FlashVar,這是以FlashVar的方式給FlashRoot添加變量,對於Number型變量在Flash端須要作類型轉換。    

so.addVariable("variable2", "value2");    

so.addVariable("variable3", "value3");    

so.addVariable("variable1", getQueryParamValue("variable1"));   //Flash獲取URL變量對於url?arg1=test1&arg2=test2這樣用GET方式傳遞變量的URL,咱們能夠用getQueryParamValue方法來獲取變量。    

so.addVariable("variable2", getQueryParamValue("variable2"));    

so.write("content");    //這裏是相當重要的一個地方,他是用一段特定內容取代Flash沒法顯示時的內容。能夠事先在CSS裏定義好樣式,在文檔裏使用div標籤寫出來。這裏在調用過來。

下面寫一個AS與JS通訊的實例:

JS端:

 

 1 <script type="text/javascript">
 2   var _file = "http://localhost:89/VideoPlayer/bin-debug/assets/3.jpg|http://localhost:89/VideoPlayer/bin-debug/assets/2.jpg";
 3   var _thumb = "http://localhost:89/VideoPlayer/bin-debug/assets/3.jpg|http://localhost:89/VideoPlayer/bin-debug/assets/2.jpg";
 4   var _wzsm = "文字1|文字2";
 5   var so = new SWFObject("MultPlayer.swf","CuPlayer","800","600","11","#00ffff");
 6   so.addParam("allowfullscreen","true");
 7   so.addParam("allowscriptaccess","always");
 8   so.addParam("wmode","opaque");
 9   so.addParam("quality","high");
10   //so.addParam("salign","lt");
11   so.addVariable("time","5000");//如自動播放圖片間隔的時間,爲0不自動播放。
12   so.addVariable("startview","0");//0表明開始顯示圖片區域,1表明開始顯示視頻區域。
13   so.addVariable("file",_file);//調用格式以「|」進行分割。
14   so.addVariable("thumb",_thumb);//視頻或者圖片圖標
15   so.addVariable("wzsm",_wzsm);//文字說明以」|」進行分割與文件一一對應點擊視頻或圖片顯示到文字區域。
16   so.addVariable("autoplay","true");//是否自動播放圖片或視頻
17   so.addVariable("videoskin","http://localhost:89/TestSwfObject/src/demo/videoskin.swf");
18   so.write("CuPlayer");//顯示到相應DIV中
19 </script>

AS端:

 

 1 package
 2 {
 3     import flash.display.Sprite;
 4     import flash.events.Event;
 5     
 6     /**
 7      * @author Frost.Yen    
 8      * @E-mail 871979853@qq.com
 9      * @create 2015-9-4 上午11:56:13
10      *
11      */
12     public class TestSWFOject extends Sprite
13     {
14         public function TestSWFOject()
15         {
16             if (stage) init();
17             else addEventListener(Event.ADDED_TO_STAGE, init);
18         }
19         private function init(e:Event = null):void 
20         {
21             removeEventListener(Event.ADDED_TO_STAGE, init);
22             this.loaderInfo.addEventListener(Event.COMPLETE,onLoaded);
23         }
24         private function onLoaded(e:Event):void
25         {
26             trace(stage.loaderInfo.parameters.time);
27             trace(stage.loaderInfo.parameters.startview);
28             trace(stage.loaderInfo.parameters.autoplay);
29             trace(stage.loaderInfo.parameters.videoskin);
30             trace(stage.loaderInfo.parameters.file);
31             trace(stage.loaderInfo.parameters.wzsm);
32             trace(stage.loaderInfo.parameters.thumb);
33         }
34     }
35 }
相關文章
相關標籤/搜索