Red5学习笔记(3)

根据网上的代码,自己整理了一下,做了一个录像的客户端。

 

 

Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"    
  3.     creationComplete="init()">  
  4.     <mx:Script>    
  5.         <![CDATA[   
  6.             import mx.controls.Alert;   
  7.             private var nc:NetConnection = null;  
  8.             private var ns:NetStream = null;  
  9.             private var video:Video = null;  
  10.             private var camera:Camera = null;//定义一个摄像头   
  11.             private var mic:Microphone; //定义一个麦克风   
  12.               
  13.             private var rtmp_url:String = "rtmp://192.168.61.115/red5_app";  
  14.             private var videoName:String = ""; //视频保存名字  
  15.               
  16.             private function init():void{  
  17.                 //nc = new NetConnection();  
  18.                 setupCameraAndMic();  //该方法表示初始化摄像头和麦克风,代码以后都可以直接复制用
  19.             }  
  20.               
  21.             private function setupCameraAndMic():void{  
  22.                 camera = Camera.getCamera();  
  23.                 if(camera != null){  
  24.                     camera.addEventListener(StatusEvent.STATUS,cameraStatus);  
  25.                     camera.setMode(320,240,30);  
  26.                     camera.setQuality(0,70);  
  27.                     video = new Video();  
  28.                     video.width = 320;  
  29.                     video.height = 240;  
  30.                     video.attachCamera(camera);  
  31.                     videoPlay.addChild(video);  
  32.                 }  
  33.                 mic = Microphone.getMicrophone();   
  34.                 if(mic != null){   
  35.                     mic.addEventListener(StatusEvent.STATUS,micStatus);  
  36.                     mic.setSilenceLevel(0,-1); //设置麦克风保持活动状态并持续接收集音频数据   
  37.                     mic.gain = 80; //设置麦克风声音大小   
  38.                 }   
  39.             }  
  40.               
  41.             private function cameraStatus(event:StatusEvent):void{  
  42.                 if(!camera.muted){  
  43.                     startRecord.enabled = true;  
  44.                     //error_label.text = error_label.text + "1";  
  45.                 }else{  
  46.                     error_label.text = error_label.text + "  无法找到摄像头"  
  47.                 }  
  48.                 //camera.removeEventListener(StatusEvent.STATUS,cameraStatus);  
  49.             }  
  50.               
  51.             /* 好像麦克风没法检测,测试过拔掉麦克风,但是没效果 */  
  52.             private function micStatus(event:StatusEvent):void{  
  53.                 if(!mic.muted){  
  54.                     startRecord.enabled = true;  
  55.                     //error_label.text = error_label.text + "2";  
  56.                 }else{  
  57.                     error_label.text = error_label.text + "  无法找到麦克风";  
  58.                 }  
  59.                 //mic.removeEventListener(StatusEvent.STATUS,micStatus);  
  60.             }  
  61.               

                     //以上代码都是直接复制过来用的,通用的,下面的代码才是真正要写的

  1.             /*点击 开始录像 按钮*/  
  2.             private function clickStartRecord():void{  
  3.                 nc = new NetConnection();  
  4.                 nc.addEventListener(NetStatusEvent.NET_STATUS,nsHandler);  
  5.                 nc.connect(rtmp_url); //连接red5  
  6.                 //doStart();  
  7.             }  
  8.               
  9.             private function nsHandler(evt:NetStatusEvent):void{  
  10.                 if(evt.info.code == "NetConnection.Connect.Success"){ //如果连接成功  
  11.                     doStart();  
  12.                 }else{  
  13.                     Alert.show("连接失败");  
  14.                 }  
  15.             }  
  16.               
  17.             /*开始录像*/  
  18.             private function doStart():void{  
  19.                 if(video != null){  
  20.                     video.clear();  
  21.                     videoPlay.removeChild(video);  
  22.                     video = new Video();  
  23.                     video.width = 320;  
  24.                     video.height = 240;  
  25.                     video.attachCamera(camera);  
  26.                     videoPlay.addChild(video);  
  27.                 }  
  28.                 ns = new NetStream(nc);  
  29.                 ns.attachCamera(camera);  
  30.                 ns.attachAudio(mic);  
  31.                 videoName = "tuping_"+Math.random()+getTimer();  
  32.                 ns.publish(videoName,"record");  
  33.                 startRecord.enabled = false;  
  34.                 stopRecord.enabled = true;  
  35.             }  
  36.               
  37.             private function clickStopRecord():void{  
  38.                 ns.close();  
  39.                 video.clear();  
  40.                 videoPlay.removeChild(video);  
  41.                 startRecord.enabled = true;  
  42.                 stopRecord.enabled = false;  
  43.                 init();/*重新初始化Camera,否则Camera会默认使用完了,不再显示图像*/  
  44.             }  
  45.         ]]>    
  46.     </mx:Script>    
  47.     <mx:Panel x="213" y="175" width="360.5" height="301" layout="absolute">  
  48.         <mx:VideoDisplay x="10" y="10" width="320" height="240" id="videoPlay"/>  
  49.     </mx:Panel>  
  50.     <mx:Button x="270" y="498" label="开始录像" id="startRecord"    
  51.         click="clickStartRecord()" enabled="false"/>  
  52.     <mx:Button x="432" y="498" label="停止录像" id="stopRecord"    
  53.         click="clickStopRecord()" enabled="false"/>  
  54.     <!--  
  55.     <mx:Button x="442" y="498" label="播放录像" id="playRecord" enabled="false"/>  
  56.     -->  
  57.     <mx:Label text="错误:" x="213" y="544" width="360.5" height="25"    
  58.         id="error_label" fontSize="12"/>  
  59.        
  60. </mx:Application>  
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
	creationComplete="init()">
	<mx:Script> 
		<![CDATA[ 
			import mx.controls.Alert; 
			private var nc:NetConnection = null;
			private var ns:NetStream = null;
			private var video:Video = null;
			private var camera:Camera = null;//定义一个摄像头 
			private var mic:Microphone; //定义一个麦克风 
			
			private var rtmp_url:String = "rtmp://192.168.61.115/red5_app";
			private var videoName:String = ""; //视频保存名字
			
			private function init():void{
				//nc = new NetConnection();
				setupCameraAndMic();
			}
			
			private function setupCameraAndMic():void{
				camera = Camera.getCamera();
				if(camera != null){
					camera.addEventListener(StatusEvent.STATUS,cameraStatus);
					camera.setMode(320,240,30);
					camera.setQuality(0,70);
					video = new Video();
					video.width = 320;
					video.height = 240;
					video.attachCamera(camera);
					videoPlay.addChild(video);
				}
				mic = Microphone.getMicrophone(); 
				if(mic != null){ 
					mic.addEventListener(StatusEvent.STATUS,micStatus);
					mic.setSilenceLevel(0,-1); //设置麦克风保持活动状态并持续接收集音频数据 
					mic.gain = 80; //设置麦克风声音大小 
				} 
			}
			
			private function cameraStatus(event:StatusEvent):void{
				if(!camera.muted){
					startRecord.enabled = true;
					//error_label.text = error_label.text + "1";
				}else{
					error_label.text = error_label.text + "  无法找到摄像头"
				}
				//camera.removeEventListener(StatusEvent.STATUS,cameraStatus);
			}
			
			/* 好像麦克风没法检测,测试过拔掉麦克风,但是没效果 */
			private function micStatus(event:StatusEvent):void{
				if(!mic.muted){
					startRecord.enabled = true;
					//error_label.text = error_label.text + "2";
				}else{
					error_label.text = error_label.text + "  无法找到麦克风";
				}
				//mic.removeEventListener(StatusEvent.STATUS,micStatus);
			}
			
			/*点击 开始录像 按钮*/
			private function clickStartRecord():void{
				nc = new NetConnection();
				nc.addEventListener(NetStatusEvent.NET_STATUS,nsHandler);
				nc.connect(rtmp_url); //连接red5
				//doStart();
			}
			
			private function nsHandler(evt:NetStatusEvent):void{
				if(evt.info.code == "NetConnection.Connect.Success"){ //如果连接成功
					doStart();
				}else{
					Alert.show("连接失败");
				}
			}
			
			/*开始录像*/
			private function doStart():void{
				if(video != null){
					video.clear();
					videoPlay.removeChild(video);
					video = new Video();
					video.width = 320;
					video.height = 240;
					video.attachCamera(camera);
					videoPlay.addChild(video);
				}
				ns = new NetStream(nc);
				ns.attachCamera(camera);
				ns.attachAudio(mic);
				videoName = "vincent_"+Math.random()+getTimer();
				ns.publish(videoName,"record");
				startRecord.enabled = false;
				stopRecord.enabled = true;
			}
			
			private function clickStopRecord():void{
				ns.close();
				video.clear();
				videoPlay.removeChild(video);
				startRecord.enabled = true;
				stopRecord.enabled = false;
				init();/*重新初始化Camera,否则Camera会默认使用完了,不再显示图像*/
			}
		]]> 
	</mx:Script> 
	<mx:Panel x="213" y="175" width="360.5" height="301" layout="absolute">
		<mx:VideoDisplay x="10" y="10" width="320" height="240" id="videoPlay"/>
	</mx:Panel>
	<mx:Button x="270" y="498" label="开始录像" id="startRecord" 
		click="clickStartRecord()" enabled="false"/>
	<mx:Button x="432" y="498" label="停止录像" id="stopRecord" 
		click="clickStopRecord()" enabled="false"/>
	<!--
	<mx:Button x="442" y="498" label="播放录像" id="playRecord" enabled="false"/>
	-->
	<mx:Label text="错误:" x="213" y="544" width="360.5" height="25" 
		id="error_label" fontSize="12"/>
	
</mx:Application>

 如果你使用Flex builder 开发,那相当简单!可以直接运行,记着插上摄像头哦!

 

忘记吧web.xml 的配置写上了,其实可以复制red5里面的例子工程,但是补上吧,怕太久了忘记了。

 

 

Xml代码 复制代码
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <web-app    
  3.    xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"    
  6.    version="2.4">    
  7.   
  8.     <display-name>Red5App</display-name>  
  9.        
  10.     <context-param>  
  11.         <param-name>Red5App</param-name>  
  12.         <param-value>/red5_app</param-value>  
  13.     </context-param>  
  14. <listener>  
  15.         <listener-class>org.red5.logging.ContextLoggingListener</listener-class>  
  16.     </listener>  
  17.        
  18.     <filter>  
  19.         <filter-name>LoggerContextFilter</filter-name>  
  20.         <filter-class>org.red5.logging.LoggerContextFilter</filter-class>  
  21.     </filter>  
  22.        
  23.     <filter-mapping>  
  24.         <filter-name>LoggerContextFilter</filter-name>  
  25.         <url-pattern>/*</url-pattern>  
  26.     </filter-mapping>  
  27.        
  28.     <servlet>  
  29.         <servlet-name>rtmpt</servlet-name>  
  30.         <servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>  
  31.         <load-on-startup>1</load-on-startup>  
  32.     </servlet>  
  33.     <!--   
  34.     <servlet>  
  35.         <display-name>SecurityTest</display-name>  
  36.         <servlet-name>SecurityTest</servlet-name>  
  37.         <servlet-class>org.red5.demos.oflaDemo.SecurityTest</servlet-class>  
  38.     </servlet>  
  39.     -->  
  40.     <servlet-mapping>  
  41.         <servlet-name>rtmpt</servlet-name>  
  42.         <url-pattern>/fcs/*</url-pattern>  
  43.     </servlet-mapping>  
  44.   
  45.     <servlet-mapping>  
  46.         <servlet-name>rtmpt</servlet-name>  
  47.         <url-pattern>/open/*</url-pattern>  
  48.     </servlet-mapping>  
  49.   
  50.     <servlet-mapping>  
  51.         <servlet-name>rtmpt</servlet-name>  
  52.         <url-pattern>/close/*</url-pattern>  
  53.     </servlet-mapping>  
  54.   
  55.     <servlet-mapping>  
  56.         <servlet-name>rtmpt</servlet-name>  
  57.         <url-pattern>/send/*</url-pattern>  
  58.     </servlet-mapping>  
  59.   
  60.     <servlet-mapping>  
  61.         <servlet-name>rtmpt</servlet-name>  
  62.         <url-pattern>/idle/*</url-pattern>  
  63.     </servlet-mapping>  
  64.     <!--   
  65.     <servlet-mapping>  
  66.         <servlet-name>SecurityTest</servlet-name>  
  67.         <url-pattern>/securitytest</url-pattern>  
  68.     </servlet-mapping>  
  69.     -->  
  70. <!--       
  71.     <security-constraint>  
  72.         <web-resource-collection>  
  73.             <web-resource-name>Forbidden</web-resource-name>  
  74.             <url-pattern>/streams/*</url-pattern>  
  75.         </web-resource-collection>  
  76.         <auth-constraint/>  
  77.     </security-constraint>  
  78. -->     
  79. </web-app>  
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
   version="2.4"> 

	<display-name>Red5App</display-name>
	
	<context-param>
		<param-name>Red5App</param-name>
		<param-value>/red5_app</param-value>
	</context-param>
<listener>
        <listener-class>org.red5.logging.ContextLoggingListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>LoggerContextFilter</filter-name>
        <filter-class>org.red5.logging.LoggerContextFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>LoggerContextFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
   	<servlet>
		<servlet-name>rtmpt</servlet-name>
		<servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!--
   	<servlet>
   		<display-name>SecurityTest</display-name>
   		<servlet-name>SecurityTest</servlet-name>
   		<servlet-class>org.red5.demos.oflaDemo.SecurityTest</servlet-class>
   	</servlet>
    -->
	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/fcs/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/open/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/close/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/send/*</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>rtmpt</servlet-name>
		<url-pattern>/idle/*</url-pattern>
	</servlet-mapping>
	<!--
	<servlet-mapping>
		<servlet-name>SecurityTest</servlet-name>
		<url-pattern>/securitytest</url-pattern>
	</servlet-mapping>
	-->
<!--    
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Forbidden</web-resource-name>
            <url-pattern>/streams/*</url-pattern>
        </web-resource-collection>
        <auth-constraint/>
    </security-constraint>
-->	
</web-app>

 

这个配置挺全了。以后可以继续使用的!

你可能感兴趣的:(Web,xml,servlet,Flex,360)