边录边播代码

在线视频录制与播放
<?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 play_url:String = "rtmp://localhost/SOSample";  //播放的地址
            
            private var nc_play:NetConnection = null;  //连接
            private var ns_play:NetStream = null;  //媒体流
            private var video_play:Video = null;  //录像
              
            private var videoName:String = ""; //录像的名字  
              
            private function init():void{  
                setupCameraAndMic();   
            }  
              
            private function setupCameraAndMic():void{  
                camera = Camera.getCamera();  
                if(camera != null){ 
                    camera.addEventListener(StatusEvent.STATUS,cameraStatus);  
                    camera.setMode(128,96,12);  
                    camera.setQuality(0,100);  
                    video = new Video();  
                    video.width = 128;  
                    video.height = 96;  
                    video.attachCamera(camera);  
                    recordVideo.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){  
                    begin.enabled = true;  
                }
            }  
              
            // 麦克风 
            private function micStatus(event:StatusEvent):void{  
                if(!mic.muted){  
                    begin.enabled = true;  
                }
            }  
              
            //点击开始录像按钮 
            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"){ //如果连接成功  
                    recordText.text = "连接成功,正在录制中...";
                    doRecord();  
                }else{  
                    Alert.show("连接失败");
                }  
            }  
              
            //开始录像 
            private function doRecord():void{  
                if(video != null){  
                    video.clear();  
                    recordVideo.removeChild(video);  
                    video = new Video();  
                    video.width = 128;  
                    video.height = 96;  
                    video.attachCamera(camera);  
                    recordVideo.addChild(video);  
                }  
                ns = new NetStream(nc);  
                ns.attachCamera(camera);  
                ns.attachAudio(mic);  
                videoName = "video_"+Math.random()+getTimer();  
                ns.publish(videoName,"record");  
                begin.enabled = false;  
                end.enabled = true;
                //一边录像一边播放  
                doPlay();     
            }  
              
              //播放
            function doPlay():void{  
                nc_play = new NetConnection();  
                nc_play.addEventListener(NetStatusEvent.NET_STATUS,ncPlayHandler);  
                nc_play.connect(play_url);  
            }  
              
              
            private function ncPlayHandler(evt:NetStatusEvent):void{  
                if(evt.info.code == "NetConnection.Connect.Success"){ 
                    error_text.text = "连接成功,正在播放中...";  
                    ns_play = new NetStream(nc_play);  
                    ns_play.play(videoName);   
                    video_play = new Video();  
                    video_play.width = 320;  
                    video_play.height = 240;  
                    video_play.attachNetStream(ns_play);  
                    playVideo.addChild(video_play);  
                }else{  
                    Alert.show("连接失败");  
                }  
            }  
              
              //结束录像和播放
            private function clickend():void{  
                ns.close();  
                video.clear();  
                ns_play.close();  
                video_play.clear();  
                recordVideo.removeChild(video);  
                begin.enabled = true;  
                end.enabled = false; 
                error_text.text=""; 
                recordText.text="";
                  
                init(); //这里一定记得要重新初始化Camera
            }  
        ]]>  
    </mx:Script>  
    
   
        
     <!--播放-->   
    <mx:Panel x="73" y="47" width="358" height="307" layout="absolute">  
        <mx:VideoDisplay x="9" y="10" width="320" height="240" id="playVideo"/>  
    </mx:Panel>  
    <mx:Text x="163" y="371" id="error_text" text="" width="183" height="35.6" fontSize="12" color="red"/>  
    <mx:Button x="120" y="424.6" enabled="false" label="开始视频" fontSize="12" id="begin" click="clickStartRecord()"/>  
    <mx:Button x="289" y="424.6" enabled="false" label="结束视频" fontSize="12" id="end" click="clickend()"/>  
    
    
     <!--录制-->
    <mx:Panel x="527" y="47" width="167" height="157.6" layout="absolute">  
        <mx:VideoDisplay x="9" y="10" width="128" height="96" id="recordVideo"/>  
    </mx:Panel>
    <mx:Text id="recordText" x="541" y="236" width="143" fontSize="12" color="red"/>
  
</mx:Application>

顺便介绍一款上网加速器利器,程序员必备!自己一直在用,有需要的朋友可以试试!
http://honx.in/i/VIFTbc6vD2Sxigzt

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值