Flex, fms3 SeriesArticleNavigation
- Index of flex and fms3 articles
This article isVideo chat, conference Development Instance series articlesThe links to all the articles in this series are as follows:
Http://www.cnblogs.com/aierong/archive/2008/12/30/Flex.html#sp
1. Working Principle
Application of netstream. Publish Method
Publish () method: Send audio streams, video streams, and text message streams from the client to the Flash Media Server, and you can choose to record the stream during transmission. This method is only used by the publisher of the specified stream.
1st parameters: the string that identifies the stream.
2nd parameters: specify how to publish the stream string. Valid values: "record", "APPEND", and "live ". The default value is "live ". (The differences between the three parameters are as follows :)
If "record" is passed, Flash Player publishes and records real-time data, and saves the recorded data to a new file whose name matches the value passed to the name parameter. This file is saved to the server applicationProgramIn the subdirectory of the directory. If the file exists, the file is overwritten.
If "APPEND" is passed, Flash Player publishes and records real-time data, and attaches the recorded data to the file whose name matches the value passed to the name parameter, the file is stored in a subdirectory on the server that contains the server application. If the file that matches the name parameter is not found, a file is created.
If this parameter is omitted or "live" is passed, Flash Player publishes real-time data without recording. Delete a file that matches the value passed to the name parameter.
For more information about the netstream. Publish method, see my previous article.
Http://www.cnblogs.com/aierong/archive/2009/01/10/flex_fms_video_start.html
The "live" type is used to implement video conferencing.
Let's take a look at the schematic diagram. The user attending the meeting will send the publish picture captured by his local camera to the FMS and store it in the collection object userlist on the FMS end, each user name is the name of the published video. Whenever a new user joins or leaves a meeting, the UDF broadcasts the userlist to each online user. To obtain the userlist, you only need to play the videos of several other users. The principle is simple!
2. Preparations
Create a new folder test_video2 in the application under the installation directory of the FMS, and then start the FMS server.
3. StartCode
This code is divided into two parts: the ASC file on the server side and the client side
There are two important classes in the ASC file:
Application class: The application class contains information about a Flash Media Server application instance, which maintains this information until the application instance is uninstalled.
Client class: the client class allows you to process every user or client connected to a Flash Media Server application instance.
The application class has the following important events:
Application. onappstart is called when the application is loaded by the server.
Application. onappstop is called when the application is uninstalled by the server.
Application. onconnect is called when a client connects to the application.
Application. ondisconnect is called when a client disconnects from the application.
The application class has the following important methods:
Application. acceptconnection () accepts a connection from a client to an application.
Application. broadcastmsg () broadcasts a message to all connected clients.
Application. Disconnect () disconnects a client from the server.
Application. rejectconnection () rejects a connection to an application.
The client class has one important method as follows:
Client. Call () asynchronously executes a method on the flash client and returns the value from the flash client to the server.
4. Write the ASC file, open the Wordpad, type the following code, save it as test_video2.asc, and copy the file to the test_video2 folder (the ASC file must have the same name as the folder or be called Main. ASC)
Userlist = [];
Application. onappstart = function ()
{
Trace ("launch an FMS server ......");
}
application. onconnect = function (currentclient)
{< br> application. acceptconnection (currentclient);
If (userlist. length >=3)
{< br> currentclient. call ("showservermsg", null, "the maximum number of users has been reached");
application. rejectconnection (currentclient);
}< br> else
{< br> currentclient. communicateserver = function (value)
{< br> currentclient. username = value;
trace (currentclient. username + "join chat room");
userlist. push (value);
trace ("current user list" + userlist);
application. broadcastmsg ("playothervideo", userlist);
}< BR >}
Application. ondisconnect = function (currentclient)
{
Trace ("user" + currentclient. username + "Leave chat room ");
For (j = 0; j <userlist. length; j ++)
{
If (userlist [J] = currentclient. username)
{
Userlist. splice (J, 1 );
}
}
Trace ("current user list" + userlist );
Application. broadcastmsg ("playothervideo", userlist );
}
Code Description:
(1) application. broadcastmsg ("client method name", parameter ..); is an important method in ASC. Its function is to broadcast to all connected clients and call the functions of the client. This method is equivalent to looping the application. the clients array and calls the client on each independent client. call (), but this method is more efficient (especially when the number of connected clients is large ). The only difference is that when you call broadcastmsg (), you cannot specify a response object. In addition, the two syntaxes are the same.
(2) The userlist. splice (a, B) function removes B elements from the array starting from position a (ASC uses AS1 syntax)
(3) restrictions are imposed in the code, and only three persons are allowed to connect to the FMS.
5. Open fb3 and create a project
6. Drag the control in the main mxml. The interface is as follows:
The Code is as follows:
<Mx: Label x = "10" Y = "10" text = "your screen"/>
<Mx: videodisplay x = "10" Y = "38" width = "120" Height = "90" id = "vd_myvideo"/>
<Mx: textinput x = "152" Y = "38" text = "enter your name" id = "txt_username" width = "116" focusin = "txt_username.text ='' "/>
<Mx: button x = "288" Y = "38" label = "Enter the chat room" id = "btn_start"/>
<Mx: vbox x = "10" Y = "161" width = "120" id = "vb_othervideo">
</MX: vbox>
7. Import the package and define the variables as follows:
Import MX. Controls. label;
Import MX. Controls. Alert;
Private var netconnection: netconnection;
Private var outnetstream: netstream;
Private var camera: camera;
Private var microphone: microphone;
Private var responder: responder;
Private var appserver: String = "rtmp: // 192.168.0.249/test_video2 ";
Private var Username: String = "";
Code Description:
Outnetstream is the video stream you want to publish to the FMS. In other words, it is your picture.
Responder if the client needs a callback function to call a method in the server-side AS1 code, define this responder.
Username: the username of each user, that is, name the video at the time of release.
8. creationcomplete = "Init ()", page initialization code
Private function Init (): void
{
Netconnection = new netconnection ();
Netconnection. addeventlistener (netstatusevent. net_status, netstatushandler );
Netconnection. Connect (appserver );
Netconnection. Client = this;
Initmedia ();
}
Private function initmedia (): void
{
Camera = camera. getcamera ();
Camera. setmode (120,90, 15 );
Camera. setquality (0, 90 );
Vd_myvideo.attachcamera (CAMERA );
Microphone = microphone. getmicrophone ();
}
Private function netstatushandler (EVT: netstatusevent): void
{
Trace (evt.info. Code );
If (evt.info. Code = "netconnection. Connect. Success ")
{
Btn_start.addeventlistener (mouseevent. Click, startcommunicate );
}
Else
{
Alert. Show ("failed to connect to the FMS" + evt.info. Code );
}
}
The code is relatively simple. If you don't understand it, see my previous article.
Http://www.cnblogs.com/aierong/archive/2009/01/10/flex_fms_video_start.html
9. Implement button events
Private function startcommunicate (EVT: mouseevent): void
{
Username = txt_username.text;
Responder = new Responder (communicatestatahandler );
Netconnection. Call ("communicateserver", responder, username );
}
Private function communicatestatahandler (STR: string): void
{
Outmyvideo ();
Btn_start.enabled = false;
}
Private function outmyvideo (): void
{
/* Publish a video */
Outnetstream = new netstream (netconnection );
Outnetstream. attachcamera (CAMERA );
Outnetstream. attachaudio (MICROPHONE );
Outnetstream. Publish (username, "live ");
}
Code Description:
The responder class provides an object that is used in netconnection. Call () to process the return values from servers related to the success or failure of a specific operation.
Netconnection. Call: Call the communicateserver In the ASC file. After the call is successful, call back the communicatestatahandler method.
10. showservermsg method implementation
Public Function showservermsg (MSG: string): void
{
Alert. Show (MSG );
}
Code Description:
For calling in ASC files
11. playothervideo method implementation:
Public Function playothervideo (newuserlist: array): void
{
Vb_othervideo.removeallchildren ();
For (var I: Int = 0; I <newuserlist. length; I ++)
{
If (newuserlist [I]! = Username)
{
VaR VD: videodisplay = new videodisplay ();
VaR video: Video = new video ();
Video. width = 120;
Video. Height = 90;
VaR innetstream: netstream = new netstream (netconnection );
Video. attachnetstream (innetstream );
Innetstream. Play (newuserlist [I]);
VaR label: Label = new label ();
Label. Text = newuserlist [I] + "image ";
VD. addchild (video );
VD. width = 120;
VD. Height = 90;
Vb_othervideo.addchild (Label );
Vb_othervideo.addchild (VD );
}
}
}
Code Description:
The application. broadcastmsg method in the ASC file is called to broadcast to all connected clients.
12. Run the program, as shown in the figure below. One of them has no video camera.
13. Download Code
Http://files.cnblogs.com/aierong/Video2.rar
After receiving the code, please return to the article and leave a message! If you don't receive it, I can send it again!
Code is provided for mutual learning and discussion! Please share more!
1. If you have any questions about the code, you can leave a message in the comment area of the article. I will do my best to reply to you!
2. If you find a bug while running the code, or have any suggestions or comments, you can leave a message in the comment area of the article and I will correct it in time!
Tips for using the comment area:
Comments in the comment area (using advanced comments) can be pasted with pictures. If there is a problem that is hard to describe, you can paste pictures and text to describe them together.
Thank you!
Favorites and sharing
Add QQ bookmarks to Baidu souzang {
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Window. Open ('HTTP: // myweb.cn.yahoo.com/popadd.html? Url = '+ encodeuricomponent (document. location. href) + '& Title =' + encodeuricomponent (document. title), 'yahoo ', 'scrollbars = Yes, width = 440, Height = 440, Left = 80, Top = 80, status = Yes, resizable = Yes ');
}
}
}
}
}
}
}
}
}
} "> Add to Yahoo favorites
RSS subscribe to me What is RSS?
Dongguan. Net Club
Welcome to join