FLEX利用FLASH MEDIA SERVER进行视频的在线录制和回放

阅读更多

安装好fms后,在Flash Media Server 2\applications目录下建立test文件夹,重新启动fms。

编写录制和回放的代码:

录制程序和界面:

xml 代码
  1. xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
  3.     <mx:Script>  
  4.          
  5.           
  6.             import mx.controls.Spacer;  
  7.             import flash.net.*;  
  8.             import flash.events.*;  
  9.             import flash.utils.*;  
  10.             import flash.media.Camera;  
  11.             import flash.media.Microphone;  
  12.             import mx.controls.*;  
  13.             //import mx.core.UIComponent;  
  14.               
  15.             //由于fms使用的是amf0而flex2中的as3默认使用的是amf3.所以要让flex使用AFM0  
  16.             NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;  
  17.             //定义一个连接  
  18.             public var netConnection:NetConnection;  
  19.             //定义一个本地视频  
  20.             public var localVideo:Video;  
  21.             //定义一个输出流  
  22.             public var outStream:NetStream = null;  
  23.             //定义一个摄像头  
  24.             public var camera:Camera;  
  25.             //定义一个麦克风  
  26.             public var microphone:Microphone;  
  27.               
  28.             //应用程序初始化  
  29.             public function initCamera():void  
  30.             {  
  31.                 //初始化连接  
  32.                 netConnection = new NetConnection();  
  33.                 //为连接添加状态监听器  
  34.                 netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);  
  35.                 //为连接添加安全错误监听器  
  36.                 netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  
  37.                 //定义一个视频持有人  
  38.                 //var videoHolder:UIComponent = new UIComponent();  
  39.                 //初始化本地视频  
  40.                 localVideo = new Video();  
  41.                 //将本地视频添加到持有人  
  42.                 //videoHolder.addChild(localVideo);  
  43.                 //设置本地视频的位置  
  44.                 myVideo.addChild(localVideo);  
  45.                 localVideo.x = 0;  
  46.                 localVideo.y = 0;  
  47.                 localVideo.width = 640;  
  48.                 localVideo.height = 480;  
  49.                   
  50.                 //连接到服务器  
  51.                 netConnection.connect("rtmp://localhost/test");  
  52.             }  
  53.               
  54.               
  55.             private function netStatusHandler(event:NetStatusEvent):void   
  56.             {  
  57.                 outStream = new NetStream(netConnection);  
  58.                 camera = Camera.getCamera();  
  59.                 camera.setMode(640,480,30);  
  60.                 microphone = Microphone.getMicrophone();  
  61.                 localVideo.attachCamera(camera);  
  62.                 outStream.attachCamera(camera);  
  63.                 outStream.attachAudio(microphone);  
  64.                 outStream.publish("testVideo","record");  
  65.                   
  66.                 Alert.show(event.info.code);  
  67.             }  
  68.             private function securityErrorHandler(event:SecurityErrorEvent):void   
  69.             {   
  70.                 Alert.show("securityErrorHandler: " + event);  
  71.             }  
  72.         ]]>  
  73.     mx:Script>  
  74.     <mx:VideoDisplay id="myVideo" x="0" y="0" height="240" width="320" creationComplete="initCamera();"/>  
  75. mx:Application>  

回放程序和界面:

xml 代码

 

  1. xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
  3.     <mx:Script>  
  4.          
  5.               
  6.             import mx.controls.Spacer;  
  7.             import flash.net.*;  
  8.             import flash.events.*;  
  9.             import flash.utils.*;  
  10.             import flash.media.Camera;  
  11.             import mx.controls.*;  
  12.               
  13.             //由于fms使用的是amf0而flex2中的as3默认使用的是amf3.所以要让flex使用AFM0  
  14.             NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;  
  15.             //定义一个连接  
  16.             public var netConnection:NetConnection;  
  17.             //定义一个本地视频  
  18.             public var localVideo:Video;  
  19.             //定义一个输入流  
  20.             public var inStream:NetStream = null;  
  21.               
  22.             //应用程序初始化  
  23.             public function initCamera():void  
  24.             {  
  25.                 //初始化连接  
  26.                 netConnection = new NetConnection();  
  27.                 //为连接添加状态监听器  
  28.                 netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);  
  29.                 //为连接添加安全错误监听器  
  30.                 netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  
  31.                 //连接到服务器  
  32.                 netConnection.connect("rtmp://localhost/test");  
  33.             }  
  34.               
  35.             private function netStatusHandler(event:NetStatusEvent):void   
  36.             {  
  37.                 //Alert.show(event.info.code);  
  38.                 switch (event.info.code)   
  39.                 {  
  40.                     case "NetConnection.Connect.Failed":  
  41.                         //Alert.show("连接失败!!");  
  42.                     case "NetConnection.Connect.Success":  
  43.                         connectStream();  
  44.                         break;  
  45.                     case "NetStream.Play.StreamNotFound":  
  46.                         //Alert.show("Stream not found: " + "rtmp://localhost/myapp");  
  47.                         break;  
  48.                 }  
  49.             }  
  50.             private function securityErrorHandler(event:SecurityErrorEvent):void   
  51.             {   
  52.                 //Alert.show("securityErrorHandler: " + event);  
  53.             }  
  54.               
  55.             private function connectStream():void   
  56.             {  
  57.                 inStream = new NetStream(netConnection);  
  58.                 inStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);  
  59.                 inStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,streamErrorHandler);  
  60.                 //初始化本地视频  
  61.                 localVideo = new Video();  
  62.                 //设置本地视频的位置  
  63.                 localVideo.x = 0;  
  64.                 localVideo.y = 0;  
  65.                 localVideo.width = 640;  
  66.                 localVideo.height = 480;  
  67.                 localVideo.attachNetStream(inStream);  
  68.                 inStream.play("testVideo");  
  69.                 myVideo.addChild(localVideo);  
  70.             }  
  71.             private function streamErrorHandler(event:AsyncErrorEvent):void  
  72.             {  
  73.                 //Alert.show(event.error.message);  
  74.             }  
  75.         ]]>  
  76.     mx:Script>  
  77.     <mx:VideoDisplay id="myVideo" x="0" y="0" height="640" width="480" creationComplete="initCamera();"/>  
  78. mx:Application>  

你可能感兴趣的:(Flash,Flex,.net,应用服务器,Adobe)