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

[Flex]两个视频netstream实现分段连续加载播放代码示例

时间:2012-09-14 11:19guying1028.iteye.com
flex两个netstream实现视频分段加载播放,flv分段连播,视频连播,flex与netstream

flex两个netstream实现视频分段加载播放

  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.             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"];  
  13.             public function init():void{  
  14.                 myVideo.urlArr = urlArr;  
  15.             }  
  16.               
  17.         ]]> 
  18.     </mx:Script> 
  19.  
  20.     <local:mVideo id="myVideo"/> 
  21.  
  22. </mx:Application> 
  23. 第二个文件:  
  24. <?xml version="1.0" encoding="utf-8"?> 
  25. <mx:Panel   
  26.           xmlns:mx="http://www.adobe.com/2006/mxml" 
  27.           creationComplete="init();"> 
  28.     <mx:Script> 
  29.         <![CDATA[  
  30.               
  31.             private var nc:NetConnection;  
  32.             private var ns:NetStream;  
  33.             private var nc2:NetConnection;  
  34.             private var ns2:NetStream;  
  35.             private var video:Video;  
  36.               
  37.             [Bindable]  
  38.             public var urlArr:Array=null;  
  39.  
  40.             private var count:int=0;  
  41.             private var finished1:int=1; //1:播放正在进行;0:播放结束  
  42.             private var finished2:int=0; //1:播放正在进行;0:播放结束  
  43.  
  44.             private function init():void  
  45.             {  
  46.  
  47.                 var nsClient:Object={};  
  48.  
  49.                 nc=new NetConnection();  
  50.                 nc.connect(null);  
  51.  
  52.                 ns=new NetStream(nc);  
  53.                 ns.play(urlArr[count]);  
  54.                 ns.client=nsClient;  
  55.                 ns.addEventListener(NetStatusEvent.NET_STATUS, myTest1);  
  56.                 video=new Video();  
  57.                 video.name="video1";  
  58.                 video.width=uic.width;  
  59.                 video.height=uic.height;  
  60.                 video.attachNetStream(ns);  
  61.                 if (uic.getChildByName("video1") != null)  
  62.                 {  
  63.                     uic.removeChild(uic.getChildByName("video1"));  
  64.                 }  
  65.  
  66.                 count++;  
  67.             }  
  68.  
  69.             private function init2():void  
  70.             {  
  71.  
  72.                 var nsClient:Object={};  
  73.  
  74.                 nc2=new NetConnection();  
  75.                 nc2.connect(null);  
  76.                 ns2=new NetStream(nc2);  
  77.                 ns2.play(urlArr[count]);  
  78.                 ns2.client=nsClient;  
  79.                 ns2.addEventListener(NetStatusEvent.NET_STATUS, myTest2);  
  80.                 video=new Video();  
  81.                 video.name="video2";  
  82.                 video.width=uic.width;  
  83.                 video.height=uic.height;  
  84.                 video.attachNetStream(ns2);  
  85.                 if (uic.getChildByName("video2") != null)  
  86.                 {  
  87.                     uic.removeChild(uic.getChildByName("video2"));  
  88.                 }  
  89.                 count++;  
  90.             }  
  91.  
  92.             private function myTest1(event:NetStatusEvent):void  
  93.             {  
  94.                 trace("count1==>>" + count);  
  95.                 trace("count1 onStatus:" + event.info.code);  
  96.                 if (event.info.code == "NetStream.Buffer.Full")  
  97.                 {  
  98.                     if (count == 1)  
  99.                     {  
  100.                         uic.addChild(video);  
  101.                         init2();  
  102.                     }  
  103.                     if (finished2 == 1)  
  104.                     {  
  105.                         ns.seek(0);  
  106.                         ns.pause();  
  107.                     }  
  108.                 }  
  109.                 else if (event.info.code == "NetStream.Play.Stop")  
  110.                 {  
  111.                     finished1=0;  
  112.                     finished2=1;  
  113.                     uic.addChild(video);  
  114.                     ns2.togglePause();  
  115.                     if (count <= urlArr.length)  
  116.                     {  
  117.                         init();  
  118.                     }  
  119.  
  120.                 }  
  121.  
  122.             }  
  123.  
  124.             private function myTest2(event:NetStatusEvent):void  
  125.             {  
  126.                 trace("count2==>>" + count);  
  127.                 trace("count2 onStatus:" + event.info.code);  
  128.                 if (event.info.code == "NetStream.Buffer.Full")  
  129.                 {  
  130.                     if (finished1 == 1)  
  131.                     {  
  132.                         ns2.seek(0);  
  133.                         ns2.pause();  
  134.                     }  
  135.                 }  
  136.                 else if (event.info.code == "NetStream.Play.Stop")  
  137.                 {  
  138.                     finished2=0;  
  139.                     finished1=1;  
  140.                     uic.addChild(video);  
  141.                     ns.togglePause();  
  142.                     if (count <= urlArr.length)  
  143.                     {  
  144.                         init2();  
  145.                     }  
  146.                 }  
  147.  
  148.             }  
  149.         ]]> 
  150.     </mx:Script> 
  151.     <mx:VideoDisplay id="uic" 
  152.                      width="550" 
  153.                      height="450"/> 
  154.  
  155.     <mx:ControlBar> 
  156.         <mx:Button label="Play/Pause" 
  157.                    click="ns.togglePause();"/> 
  158.         <mx:Button label="Rewind" 
  159.                    click="ns.seek(0); ns.pause();"/> 
  160.     </mx:ControlBar> 
  161. </mx:Panel> 

 

热门文章推荐

请稍候...

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

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