使用Flex 和 Red5开发简易视频直播功能

1,服务器端可以空实现ApplicationAdapter:示例如下:

 

[java]  view plain copy
  1. package org.chy.flex01;  
  2.   
  3. import org.red5.server.adapter.ApplicationAdapter;  
  4.   
  5. public class Application extends ApplicationAdapter{  
  6.       
  7.   
  8. }  

客户端使用Flex实现摄像头获取视频并发布代码如下:

 

[javascript]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">  
  3.     <mx:Script>  
  4.         <!--[CDATA[  
  5.             import flash.net.NetConnection;  
  6.             import flash.net.NetStream;  
  7.             import flash.events.NetStatusEvent;  
  8.             import mx.controls.Alert;  
  9.               
  10.             private var nc:NetConnection;  
  11.             private var ns:NetStream;  
  12.             private var cam:Camera;  
  13.               
  14.             private function init():void{  
  15.                 nc = new NetConnection();  
  16.                 nc.addEventListener(NetStatusEvent.NET_STATUS,connectServerHander);  
  17.                 nc.client  = this;  
  18.                 nc.connect("rtmp://58.116.56.68:1935/Flex01");  
  19.             }  
  20.               
  21.             private function connectServerHander(evt:NetStatusEvent):void{  
  22.                 trace(evt.info.code);   
  23.                 cam = Camera.getCamera();  
  24.                   
  25.                 if(cam != null){  
  26.                     cam.setMode(313,194,30);  
  27.                     cam.setQuality(0,70);  
  28.                     ns = new NetStream(nc);   
  29.                     ns.attachCamera(cam);  
  30.                     ns.publish("red5");  
  31.                       
  32.                     var vi:Video = new Video();  
  33.                     vi.width = 313;  
  34.                     vi.height = 194;  
  35.                     vi.attachCamera(cam);  
  36.                     videoDisplay.addChild(vi);  
  37.                       
  38.                 }else{  
  39.                     Alert.show("no Camera");  
  40.                 }  
  41.             }  
  42.                           
  43.             public function onBWDone():void{  
  44.               
  45.             }  
  46.               
  47.         ]]-->  
  48.     </mx:Script>  
  49.       
  50.     <mx:VideoDisplay x="162" y="77" width="313" height="194" id="videoDisplay"/>  
  51.       
  52. </mx:Application>  

客户端使用Flex play视频代码如下:

[javascript]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">  
  3.   
  4.     <mx:Script>  
  5.         <!--[CDATA[  
  6.               
  7.             import flash.display.*;  
  8.             import flash.utils.*;  
  9.             import flash.events.*;  
  10.             import flash.net.*;  
  11.             import flash.media.Video;  
  12.             import flash.media.Camera;  
  13.               
  14.             private var nc:NetConnection;  
  15.               
  16.             private function init():void{  
  17.                 nc = new NetConnection();  
  18.                 nc.connect("rtmp://58.116.56.68:1935/Flex01");  
  19.                 nc.addEventListener(NetStatusEvent.NET_STATUS,connectHandler);  
  20.             }  
  21.   
  22.             private function connectHandler(evt:NetStatusEvent):void{  
  23.                   
  24.                 var ns:NetStream = new NetStream(nc);  
  25.                 var video:Video=new Video() ;  
  26.                 video.width = 313;  
  27.                 video.height = 194;  
  28.                 video.attachNetStream(ns);  
  29.                 ns.play("red5");  
  30.                 videoDisplay.addChild(video);  
  31.                   
  32.             }  
  33.               
  34.         ]]-->  
  35.     </mx:Script>  
  36.       
  37.     <mx:Panel x="132" y="78" width="356" height="266" layout="absolute" title="现场直播" fontSize="14">  
  38.         <mx:VideoDisplay x="10" y="10" width="320" height="193" id="videoDisplay"/>  
  39.     </mx:Panel>  
  40.       
  41. </mx:Application>  

package chat; import org.red5.server.adapter.ApplicationAdapter; import org.red5.server.api.IConnection; import org.red5.server.api.IClient; import org.red5.server.api.IScope; import org.red5.server.api.Red5; import org.red5.server.api.service.IServiceCapableConnection; import org.red5.server.api.so.ISharedObject; import org.red5.server.api.ScopeUtils; import org.red5.server.api.so.ISharedObjectService; import org.red5.server.api.*; import org.red5.server.api.scheduling.*; import org.red5.samples.components.ClientManager; import org.red5.server.api.service.ServiceUtils; import org.red5.server.api.service.*; import org.red5.server.api.service.IPendingServiceCallback; // import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.util.*; // // public class Application extends ApplicationAdapter{ public Map<String, CClient> clients = new HashMap<String, CClient>(); public class CClient{ String id = null; String pseudo = null; String role = null; String sexe = null; String status = null; String room=null; String world=null; public String getRole(){ return role; } public String getName(){ return pseudo; } public String getId(){ return id; } public String getSexe(){ return sexe; } public String getStatus(){ return status; } public String getRoom(){ return room; } public String getWorld(){ return world; } public void setRole(String _role){ role = _role; } public void setPseudo(String _pseudo){ pseudo = _pseudo; } public void setSexe(String _sexe){ sexe = _sexe; } public void setId(String _id){ id = _id; } public void setStatus(String _status){ status = _status; } public void setRoom(String _room){ room = _room; } public void setWorld(String _world){ world = _world; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值