·您当前的位置:首页 > 技术教程 > 播放器教程 >

[Flex]flex的NetStream视频用SoundTransform控制音量大小代码示

时间:2012-09-14 11:25CuPlayer
[Flex]NetStream动态加载视频利用SoundTransform控制音量,flex与NetStream,flex播放器

[Flex]NetStream动态加载视频利用SoundTransform控制音量

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!-- http://blog.flexexamples.com/2008/03/01/displaying-a-video-in-flex-using-the-netconnection-netstream-and-video-classes/ --> 
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
  4.                 layout="vertical" 
  5.                 verticalAlign="middle" 
  6.                 backgroundColor="white" 
  7.                 viewSourceURL="srcview/index.html" xmlns:local="*" 
  8.                 initialize="init();"> 
  9.  
  10.     <mx:Script> 
  11.         <![CDATA[  
  12.             import mx.controls.Alert;  
  13.             private var urlArr:Array=["http://221.122.36.143/oa/video/1.flv", "http://221.122.36.143/oa/video/2.flv", "http://221.122.36.143/oa/video/p2.mp4"];  
  14.             public function init():void{  
  15.                 myVideo.urlArr = urlArr;  
  16.                 myVideo.total = 100000;  
  17. //              Alert.show("1==>>"+myVideo.urlArr.length);  
  18.             }  
  19.               
  20.         ]]> 
  21.     </mx:Script> 
  22.  
  23.     <local:mVideo id="myVideo"/> 
  24.  
  25. </mx:Application> 
  26.  
  27. 第二个文件  
  28. <?xml version="1.0" encoding="utf-8"?> 
  29. <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" 
  30.           creationComplete="setTransformVolume();"> 
  31.     <mx:Script> 
  32.         <![CDATA[  
  33.             import mx.controls.Alert;  
  34.             import mx.events.SliderEvent;  
  35.  
  36.             private var nc:NetConnection;  
  37.             private var ns:NetStream;  
  38.             private var nc2:NetConnection;  
  39.             private var ns2:NetStream;  
  40.             private var video:Video;  
  41.  
  42.             [Bindable]  
  43.             public var urlArr:Array=null;  
  44.             [Bindable]  
  45.             public var total:Number=0;  
  46.  
  47.             private var count:int=0;  
  48.             private var finished1:int=1; //1:播放正在进行;0:播放结束  
  49.             private var finished2:int=0; //1:播放正在进行;0:播放结束  
  50.  
  51.             private var volumeTransform:SoundTransform;  
  52.  
  53.               
  54.             private function setTransformVolume():void{  
  55.                 volumeTransform=new SoundTransform();  
  56.                 slider.value = volumeTransform.volume;  
  57.                 slider.tickInterval = slider.snapInterval;  
  58.                 slider.liveDragging = true;  
  59.                 slider.addEventListener(Event.CHANGE, volumeChangeHandler);  
  60.                 init();  
  61.             }  
  62.               
  63.             private function init():void  
  64.             {  
  65.                 Alert.show("" + total);  
  66.                   
  67.                 var nsClient:Object={};  
  68.                 nc=new NetConnection();  
  69.                 nc.connect(null);  
  70.                 ns=new NetStream(nc);  
  71.                 ns.play(urlArr[count]);  
  72.                 ns.client=nsClient;  
  73.                 ns.soundTransform=volumeTransform;  
  74.                 ns.addEventListener(NetStatusEvent.NET_STATUS, myTest1);  
  75.                   
  76.                 video=new Video();  
  77.                 video.name="video1";  
  78.                 video.width=uic.width;  
  79.                 video.height=uic.height;  
  80.                 video.attachNetStream(ns);  
  81.                 if (uic.getChildByName("video1") != null)  
  82.                 {  
  83.                     uic.removeChild(uic.getChildByName("video1"));  
  84.                 }  
  85.  
  86.                 count++;  
  87.             }  
  88.             private function volumeChangeHandler(event:SliderEvent):void {  
  89.                 volumeTransform.volume = slider.value;  
  90.                 ns.soundTransform = volumeTransform;  
  91.                 ns2.soundTransform = volumeTransform;  
  92.             }  
  93.             private function init2():void  
  94.             {  
  95.  
  96.                 var nsClient:Object={};  
  97.  
  98.                 nc2=new NetConnection();  
  99.                 nc2.connect(null);  
  100.                 ns2=new NetStream(nc2);  
  101.                 ns2.play(urlArr[count]);  
  102.                 ns2.client=nsClient;  
  103.                 ns2.soundTransform=volumeTransform;  
  104.                 ns2.addEventListener(NetStatusEvent.NET_STATUS, myTest2);  
  105.                   
  106.                 video=new Video();  
  107.                 video.name="video2";  
  108.                 video.width=uic.width;  
  109.                 video.height=uic.height;  
  110.                 video.attachNetStream(ns2);  
  111.                 if (uic.getChildByName("video2") != null)  
  112.                 {  
  113.                     uic.removeChild(uic.getChildByName("video2"));  
  114.                 }  
  115.                 count++;  
  116.             }  
  117.  
  118.             private function myTest1(event:NetStatusEvent):void  
  119.             {  
  120.                 trace("count1==>>" + count);  
  121.                 trace("count1 onStatus:" + event.info.code);  
  122.                 if (event.info.code == "NetStream.Buffer.Full")  
  123.                 {  
  124.                     if (count == 1)  
  125.                     {  
  126.                         uic.addChild(video);  
  127.                         init2();  
  128.                     }  
  129.                     if (finished2 == 1)  
  130.                     {  
  131.                         ns.seek(0);  
  132.                         ns.pause();  
  133.                     }  
  134.                 }  
  135.                 else if (event.info.code == "NetStream.Play.Stop")  
  136.                 {  
  137.                     finished1=0;  
  138.                     finished2=1;  
  139.                     uic.addChild(video);  
  140.                     ns2.togglePause();  
  141.                     if (count <= urlArr.length)  
  142.                     {  
  143.                         init();  
  144.                     }  
  145.  
  146.                 }  
  147.  
  148.             }  
  149.  
  150.             private function myTest2(event:NetStatusEvent):void  
  151.             {  
  152.                 trace("count2==>>" + count);  
  153.                 trace("count2 onStatus:" + event.info.code);  
  154.                 if (event.info.code == "NetStream.Buffer.Full")  
  155.                 {  
  156.                     if (finished1 == 1)  
  157.                     {  
  158.                         ns2.seek(0);  
  159.                         ns2.pause();  
  160.                     }  
  161.                 }  
  162.                 else if (event.info.code == "NetStream.Play.Stop")  
  163.                 {  
  164.                     finished2=0;  
  165.                     finished1=1;  
  166.                     uic.addChild(video);  
  167.                     ns.togglePause();  
  168.                     if (count <= urlArr.length)  
  169.                     {  
  170.                         init2();  
  171.                     }  
  172.                 }  
  173.  
  174.             }  
  175.  
  176.             public function stopVideo():void  
  177.             {  
  178.                 uic.stop();  
  179.             }  
  180.         ]]> 
  181.     </mx:Script> 
  182.     <mx:VideoDisplay id="uic" 
  183.                      width="550" 
  184.                      height="450" 
  185.                      volume="{slider.value}"/> 
  186.  
  187.     <mx:ControlBar> 
  188.         <mx:HSlider id="slider" 
  189.                     minimum="0.0" 
  190.                     maximum="1.0" 
  191.                     snapInterval="0.1" 
  192.                     tickInterval="0.1" 
  193.                     liveDragging="true"/> 
  194.         <mx:Button label="Play/Pause" 
  195.                    click="uic.play();"/> 
  196.         <mx:Button label="Rewind" 
  197.                    click="stopVideo();"/> 
  198.     </mx:ControlBar> 
  199. </mx:Panel> 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器