<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="connectFMS()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var vi:Video;
private var cam:Camera;
private var outStream:NetStream;
private var nc:NetConnection;
private var connectionURL:String="rtmp://192.168.1.101:1935/test";
private function connectFMS():void{
//创建连接
nc = new NetConnection();
//回调为本机客户端
nc.client = this;
//根据url连接
nc.connect(connectionURL);
//添加 状态变化处理函数
nc.addEventListener(NetStatusEvent.NET_STATUS,ncHandler);
}
private function ncHandler(evt:NetStatusEvent):void
{
//如果连接成功
if (evt.info.code == "NetConnection.Connect.Success")
{
Alert.show("连接成功", "提示");
//录制视频
beginRec();
}
else
{
Alert.show("连接失败", "提示");
}
}
private function beginRec():void{
//得到当前本机视频设备的流对象
cam = Camera.getCamera();
if(cam == null){
Alert.show("无摄像头","提示");
}else{
//大小与帧率
cam.setMode(320,240,30);
//设置清晰度
cam.setQuality(0,100);
//输出流对象 封装fms服务器连接
outStream = new NetStream(nc);
//服务器回调客户端为本机
outStream.client = this;
//激活视频
outStream.attachCamera(cam);
//发布 参数一为视频name 参数二为视频传送策略
outStream.publish("1111","live");
//监听流的状态
outStream.addEventListener(NetStatusEvent.NET_STATUS,netHandler);
}
}
private function netHandler(evt:NetStatusEvent):void{
//如果已经开始
if(evt.info.code == "NetStream.Publish.Start"){
//从FMS服务器得到视频流
getVideo();
}
}
private function getVideo():void
{
//创建输入流 封装fms连接
var inStream:NetStream=new NetStream(nc);
//服务器回调为本机
inStream.client=this;
//订阅名字为connectName的流对象
inStream.play("1111"); //1111为流的名字,对应于视频流发布端的publish("1111","live").
//创建视频播放对象
vi=new Video();
vi.width=viewDisplay.width;
vi.height=viewDisplay.height;
vi.smoothing=true;
//封装输入流
vi.attachNetStream(inStream);
//加入到界面上的播放器
viewDisplay.addChild(vi);
}
]]>
</mx:Script>
<mx:Button label="发送视频" click=" beginRec()"/>
<mx:VideoDisplay id="viewDisplay" x="141" y="125" width="150" height="150"/>
<mx:Button x="141" y="95" label="开始接受视频流" click="getVideo()"/>
</mx:Application>
AS3和FMS的视频录制小例子
最新推荐文章于 2017-10-10 16:47:07 发布