HTML向Flex传参

最近有个需求,要在网站中播放一段介绍性的视频,于是做了一个用Flex播放视频的demo。播放视频的代码是以前在网上找的,但是从html页面获得视频地址的时候参数传递问题浪费了很多时间,现整理总结如下:

在Flex中接收参数很简单,通过FlexGlobals.topLevelApplication.parameters或直接用parameters,文档中对parameters的介绍如下:

spark.components.Application.parameters():Object
[只读] 包含表示提供给此 Application 的参数的名称-值对的 Object。

可以使用 for-in 循环来提取 parameters Object 中的所有名称和值。

参数有两个源:Application 的 URL 中的查询字符串和 FlashVars HTML 参数(仅适用于主 Application)的值。

 

 

 

接收参数代码:

  1. privatefunction init():void 
  2.     if (parameters != null) { 
  3.         var source:String = parameters.source as String; 
  4.         var param2:String = parameters.test as String; 
  5.     } 
			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&param2=param2代码如下:

 

  1. swfobject.embedSWF( 
  2.                 "VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv&param2=test", "flashContent",  
  3.                 "500", "500",  
  4.                 swfVersionStr, xiSwfUrlStr,  
  5.                 flashvars, params, attributes); 
swfobject.embedSWF(
                "VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv&param2=test", "flashContent", 
                "500", "500", 
                swfVersionStr, xiSwfUrlStr, 
                flashvars, params, attributes);
  1. <noscript> 
  2.     <objectclassid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"width="500"height="500"id="VideoPlayer"> 
  3.       <paramname="movie"value="VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv"/> 
  4.       ...... 
  5.       <!--[if !IE]>--> 
  6.       <objecttype="application/x-shockwave-flash"data="VideoPlayer.swf?source=http://material.157you.com/material/767/dcf_enc.flv&param2=test "width="500"height="500"> 
  7.          ....... 
  8.       <!--<![endif]--> 
  9.       ...... 
  10.     </object> 
  11. <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&param2=test " width="500" height="500">
         .......
      <!--<![endif]-->
      ......
	</object>
<noscript>

        2.通过FlashVars传递参数

 

 

  1. var flashvars = {}; 
  2. flashvars.source = "http://material.157you.com/material/767/dcf_enc.flv"
  3. flashvars.param2 = "test"
  4.  
  5. swfobject.embedSWF( 
  6.     "VideoPlayer.swf", "flashContent",  
  7.     "500", "500",  
  8.     swfVersionStr, xiSwfUrlStr,  
  9.     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);
  1. <noscript> 
  2.     <objectclassid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"width="500"height="500"id="VideoPlayer"> 
  3.         <paramname="movie"value="VideoPlayer.swf"/> 
  4.         <paramname="flashvars"value="source=http://material.157you.com/material/767/dcf_enc.flv&param2=test"/> 
  5.      ...... 
  6.         <!--[if !IE]>--> 
  7.         <objecttype="application/x-shockwave-flash"data="VideoPlayer.swf"width="500"height="500"> 
  8.             <paramname="flashvars"value="source=http://material.157you.com/material/767/dcf_enc.flv&param2=test"/> 
  9.             ...... 
  10.         <!--<![endif]--> 
  11.         ...... 
  12.     </object> 
  13. <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&param2=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&param2=test" />
		    ......
		<!--<![endif]-->
		......
	</object>
<noscript>

注意:当同时使用url和flashvars传参时,如果参数名相同则flashvars的值会覆盖url的值

 

        以上是通过Flex提供的专门传递参数的方式实现的,还有一种方式也可以实现参数传递,我们知道在Flex中可以调用JS的方法,所以可以在JS中写一个方法,返回值为要传递的参数,这样在Flex中就可以获取到了,这算是曲线救国的一种方法。

JS方法,返回参数params:

  1. function flexParams() { 
  2.     var params = {param2:‘test‘}; 
  3.     params.source = "http://material.157you.com/material/767/dcf_enc.flv"
  4.  
  5.     return params; 
			function flexParams() {
				var params = {param2:‘test‘};
				params.source = "http://material.157you.com/material/767/dcf_enc.flv";

				return params;
			}

Flex中调用JS方法,获得参数:

 

  1. privatefunction init():void 
  2.     var params:Object = ExternalInterface.call("flexParams"); 
  3.      
  4.     if (params != null) { 
  5.         var source:String = params.source as String; 
  6.         var param2:String = params.param2 as String; 
  7.     } 
			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;
				}
			}

HTML向Flex传参,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。