<?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