[FMS] 스트림 녹화 (Recording stream using Flex,FMS)
Stream녹화하는 예제를 찾는 중, Red5를 이용한 예제는 수없이 많았지만 FMS를 이용한 예제를 찾기 힘들어 포스팅 합니다.
Server Side소스는 따로 필요없습니다. rtmp PATH만 잡아주시면 됩니다. 핵심은 Stream을 퍼블리싱 할 때, 두번째 값으로 "record"를 넘겨주시면 됩니다.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955"
minHeight="600"
creationComplete="initApp();"
>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private var ns:NetStream = null;
private var nc:NetConnection = null;
private var cam:Camera = null;
private var mic:Microphone = null;
private var vid1:Video;
private var vid2:Video;
private var msg:Boolean;
private var metaSniffer:Object;
private function initApp():void
{
initDevice();
initConnection();
videoSetup();
recordBtn.addEventListener (MouseEvent.CLICK,startRecord);
stopBtn.addEventListener (MouseEvent.CLICK,stopAll);
playBtn.addEventListener (MouseEvent.CLICK,startPlay);
}
private function initDevice():void
{
cam = Camera.getCamera();
cam.setKeyFrameInterval(12);
cam.setMode(240, 180, 15);
cam.setQuality(0, 80);
mic = Microphone.getMicrophone();
}
private function initConnection():void
{
nc = new NetConnection();
nc.connect('rtmp://localhost/RecordTest/');
nc.addEventListener("netStatus", checkConnect);
}
private function videoSetup():void
{
vid1 = new Video(cam.width, cam.height);
vid1.attachCamera(cam);
currentVideo.addChild(vid1);
vid2 = new Video(cam.width, cam.height);
recordedVideo.addChild(vid2);
}
private function checkConnect (e:NetStatusEvent):void
{
msg=(e.info.code=="NetConnection.Connect.Success");
if (msg)
{
ns = new NetStream(nc);
metaSniffer=new Object();
ns.client=metaSniffer;
metaSniffer.onMetaData=getMeta;
}
}
private function getMeta (mdata:Object):void
{
//Dummy to avoid error
}
private function startRecord(e:Event):void
{
if(ns)
{
recordBtn.label = "Recording";
ns.attachAudio(mic);
ns.attachCamera(cam);
ns.publish("myStream", "record");
}
}
private function stopAll(e:Event):void
{
playBtn.label = "Playing";
recordBtn.label = "Record";
ns.close();
}
private function startPlay(e:Event):void
{
if(ns)
{
playBtn.label="Playing";
vid2.attachNetStream(ns);
ns.play("myStream");
}
}
]]>
</fx:Script>
<s:VGroup>
<s:HGroup>
<s:VideoDisplay width="300" height="300" id="currentVideo" />
<s:VideoDisplay width="300" height="300" id="recordedVideo" />
</s:HGroup>
<s:HGroup>
<s:Button label="Record" id="recordBtn" />
<s:Button label="Stop" id="stopBtn" />
<s:Button label="Play" id="playBtn" />
</s:HGroup>
</s:VGroup>
</s:Application>
*source code :RecordTest.fxp