In the previous articles in this series, we have introduced the functions of connecting FMS server, setting up playback program and online video recording and playback. I believe that I have seen several articles in the previous friends have a certain understanding of FMS, and familiar with the common programming model. This article will be combined with the previous several articles appear technical points, to achieve a video chat program.
In fact, it is very simple to realize video chatting through FMS, that is, the operation is always flowing. If it is a one-way video chat, both ends of the side for the publication side for the subscriber side, if the two-way video chat, both sides are the publisher and the Subscriber side.
If the analysis from the technical implementation, one-way video chat is one side of the release stream on the other side of the stream, two-way video chat is both sides need to provide two streams, one responsible for publishing the stream, one responsible for the playback stream. In the said professional point is a create stream and send to the server's client called publish, a create stream to accept the content of the client called subscription, when the same client is the same as the publication and subscription, it must create two streams, one is the output stream, one is the receiving stream.
Said so much below to see how specific is achieved, to achieve video chat above analysis, is the side of the release video streaming side of the broadcast, which is inseparable from the link FMS, the code is as follows:
private function onPublishClick(evt:MouseEvent):void
{
nc = new NetConnection();
nc.connect("rtmp://localhost/LiveStreams");
nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler);
}
The purpose of the video release is achieved by clicking on the Netconnection FMS server and then releasing the video stream to the FMS (publish). It is important to note here that the latter parameter in the Publish method publish () is "Live", which indicates the streaming of video from time to time. Streaming in live does not generate the. fla file in FMS, unlike the record recording video stream generation. flv video file.
private function onNetStatusHandler(evt:NetStatusEvent):void
{
trace(evt.info.code);
if(evt.info.code=="NetConnection.Connect.Success")
{
ns=new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler);
ns.client=new CustomClient();
ns.attachCamera(cam);
ns.attachAudio(mic);
ns.publish(txtInput.text,"live");
}
}
The key technology to realize the video publishing is to obtain the video and audio data, respectively, through the static method of camera and microphone. Reference code:
public function PublishStream():void
{
btnPublish.label="发布视频";
btnPublish.addEventListener(MouseEvent.CLICK,onPublishClick);
//获取视频和声音,并将视频显示到Flash界面
cam = Camera.getCamera();
mic = Microphone.getMicrophone();
video = new Video(320,240);
video.attachCamera(cam);
video.x=20;
video.y=20;
addChild(video);
}