录像代码

<?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://localhost/SOSample"; 
            private var videoName:String = ""; //视频保存名字 
             
             
             //初始化Camera和mic
            private function init():void{  
                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; 
                }else{ 
                    error_label.text = error_label.text + "  无法找到摄像头" 
                } 
            } 
             
            /* 麦克风*/ 
            private function micStatus(event:StatusEvent):void{ 
                if(!mic.muted){ 
                    startRecord.enabled = true; 
                }else{ 
                    error_label.text = error_label.text + "  无法找到麦克风"; 
                } 
            } 
             
            /*点击 开始录像 按钮*/ 
            private function clickStartRecord():void{ 
                nc = new NetConnection(); 
                nc.addEventListener(NetStatusEvent.NET_STATUS,nsHandler); 
                nc.connect(rtmp_url); //连接red5 
            } 
             
              /*连接*/
            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 = "video_"+Math.random()+getTimer();   //视频的名字  video_0.1212323211323243
                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
            } 
        ]]>   
    </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:Label text="" x="213" y="544" width="360.5" height="25"   
        id="error_label" fontSize="12"/> 


      
</mx:Application> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值