以下是运用FMS录制视频的一段代码:
package { import flash.display.Sprite; import flash.events.AsyncErrorEvent; import flash.events.NetStatusEvent; import flash.events.StatusEvent; import flash.media.Camera; import flash.media.Microphone; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; import flash.net.ObjectEncoding; import flash.utils.flash_proxy; /** * @author: Frost.Yen * @E-mail: 871979853@qq.com * @create: 2017-1-1 下午4:27:50 * */ [SWF(width="800",height="500")] public class RecorderTest extends Sprite { private var _netConnection:NetConnection = new NetConnection(); private var _netStream:NetStream = null; private var _fms:String = "rtmp://localhost/yen";//{FMS-Install-Dir}/applications目录下的一个文件夹yen private var _camera:Camera = null; private var _microphone:Microphone = null; private var _video:Video; public function RecorderTest() { init(); } private function init():void { _netConnection.client = { onBWDone: function():void{} }; _netConnection.objectEncoding = ObjectEncoding.AMF3; _netConnection.connect(_fms); _netConnection.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus); } private function onNetStatus(e:NetStatusEvent):void { trace(e.info.code); switch(e.info.code) { case "NetConnection.Connect.Success"://连接尝试成功 publishStream();//发布流到服务器 break; case "NetConnection.Connect.Closed"://成功关闭连接 break; case "NetConnection.Connect.Rejected" : trace("没有权限"); break; default: trace("连接服务器失败"); break; } } private function publishStream():void { _netStream = new NetStream(_netConnection);//创建于FMS连接相关的音视频数据流对象 _netStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); _camera = Camera.getCamera("0");//获得默认摄像头 _camera.setMode(640,480,15);//经试验:320*240效果较佳而占用网络流量较小,15为keyFrame _camera.setQuality(0,100);//100*1000为上传带宽,90为图像质量; _microphone = Microphone.getMicrophone();//获得麦克风对象 _microphone.setUseEchoSuppression(true);//抑制回音; _netStream.attachAudio(_microphone);//将音频加入到流中; _netStream.attachCamera(_camera);//将视频加入到流中; _netStream.publish("my_video","record");//将音视频数据发布到FMS服务器上,流名称为:my_video,发布类型为:live(实时); _video = new Video(640,480); _video.attachCamera(_camera); _video.smoothing = true; this.addChild(_video); } private function asyncErrorHandler(e:AsyncErrorEvent):void { trace(e.toString()); } } }
假如状态代码返回"NetConnection.Connect.Rejected",我们需要检查是否自己创建的服务器目录有问题,即yen文件夹,FMS安装后applications目录下一般会有四个子目录,我们将live目录下的main.far、Application.xml、allowedHTMLDomains.txt和allowedSWFDomains.txt这四个文件复制到所创建的FMS服务器目录(yen)下即可,至于为何,可以阅读live下的readme.txt。