最近有个需求,要在网站中播放一段介绍性的视频,于是做了一个用Flex播放视频的demo。播放视频的代码是以前在网上找的,但是从html页面获得视频地址的时候参数传递问题浪费了很多时间,现整理总结如下:
在Flex中接收参数很简单,通过FlexGlobals.topLevelApplication.parameters或直接用parameters,文档中对parameters的介绍如下:
接收参数代码:
- privatefunction init():void
- {
- if (parameters != null) {
- var source:String = parameters.source as String;
- var param2:String = parameters.test as String;
- }
- }
private function init():void { if (parameters != null) { var source:String = parameters.source as String; var param2:String = parameters.test as String; } }
parameters 参数有两个来源Url和FlashVars,分别介绍如下:
新建一个Flex应用程序时会同时新建一个html页面,Flex应用是嵌入到html中的。其中包括javascript和<noscript>标签两部分,只有当浏览器不支持javascript时才会执行<noscript>中的代码。
1.通过url传递参数
这种方式和html url传参一样,swf?param1=param1¶m2=param2代码如下:
- swfobject.embedSWF(
- "VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv¶m2=test", "flashContent",
- "500", "500",
- swfVersionStr, xiSwfUrlStr,
- flashvars, params, attributes);
swfobject.embedSWF( "VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv¶m2=test", "flashContent", "500", "500", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);
- <noscript>
- <objectclassid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"width="500"height="500"id="VideoPlayer">
- <paramname="movie"value="VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv"/>
- ......
- <!--[if !IE]>-->
- <objecttype="application/x-shockwave-flash"data="VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv¶m2=test "width="500"height="500">
- .......
- <!--<![endif]-->
- ......
- </object>
- <noscript>
<noscript> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="500" height="500" id="VideoPlayer"> <param name="movie" value="VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv" /> ...... <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv¶m2=test " width="500" height="500"> ....... <!--<![endif]--> ...... </object> <noscript>
2.通过FlashVars传递参数
- var flashvars = {};
- flashvars.source = "http://material.157you.com/material/767/dcf_enc.flv";
- flashvars.param2 = "test";
- swfobject.embedSWF(
- "VideoPlayer.swf", "flashContent",
- "500", "500",
- swfVersionStr, xiSwfUrlStr,
- flashvars, params, attributes);
var flashvars = {}; flashvars.source = "http://material.157you.com/material/767/dcf_enc.flv"; flashvars.param2 = "test"; swfobject.embedSWF( "VideoPlayer.swf", "flashContent", "500", "500", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);
- <noscript>
- <objectclassid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"width="500"height="500"id="VideoPlayer">
- <paramname="movie"value="VideoPlayer.swf"/>
- <paramname="flashvars"value="source=http://material.157you.com/material/767/dcf_enc.flv¶m2=test"/>
- ......
- <!--[if !IE]>-->
- <objecttype="application/x-shockwave-flash"data="VideoPlayer.swf"width="500"height="500">
- <paramname="flashvars"value="source=http://material.157you.com/material/767/dcf_enc.flv¶m2=test"/>
- ......
- <!--<![endif]-->
- ......
- </object>
- <noscript>
<noscript> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="500" height="500" id="VideoPlayer"> <param name="movie" value="VideoPlayer.swf" /> <param name="flashvars" value="source=http://material.157you.com/material/767/dcf_enc.flv¶m2=test" /> ...... <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="VideoPlayer.swf" width="500" height="500"> <param name="flashvars" value="source=http://material.157you.com/material/767/dcf_enc.flv¶m2=test" /> ...... <!--<![endif]--> ...... </object> <noscript>
注意:当同时使用url和flashvars传参时,如果参数名相同则flashvars的值会覆盖url的值
以上是通过Flex提供的专门传递参数的方式实现的,还有一种方式也可以实现参数传递,我们知道在Flex中可以调用JS的方法,所以可以在JS中写一个方法,返回值为要传递的参数,这样在Flex中就可以获取到了,这算是曲线救国的一种方法。
JS方法,返回参数params:
- function flexParams() {
- var params = {param2:‘test‘};
- params.source = "http://material.157you.com/material/767/dcf_enc.flv";
- return params;
- }
function flexParams() { var params = {param2:‘test‘}; params.source = "http://material.157you.com/material/767/dcf_enc.flv"; return params; }
Flex中调用JS方法,获得参数:
- privatefunction init():void
- {
- var params:Object = ExternalInterface.call("flexParams");
- if (params != null) {
- var source:String = params.source as String;
- var param2:String = params.param2 as String;
- }
- }
private function init():void { var params:Object = ExternalInterface.call("flexParams"); if (params != null) { var source:String = params.source as String; var param2:String = params.param2 as String; } }