FMS3 and Flex create online video recording and playback

Source: Internet
Author: User

FLEX,FMS3 Series article Navigation
  1. FLEX,FMS3 related articles Index

This is a video chat, Conference development Example series of the 2nd article, the series of all articles linked as follows:
Http://www.cnblogs.com/aierong/archive/2008/12/30/Flex.html#sp

1. The simple application of FMS3 and flex can be seen in my previous article
Http://www.cnblogs.com/aierong/archive/2009/01/09/flex_fms_chat.html

2. Preparatory work
In the application of the FMS installation directory, create a new folder Test_video1 and start the FMS server

3. Open FB3, create a project

4. Drag the control in main mxml with the following interface:

The specific code is as follows:
<mx:videodisplay x= "y=", "height=", "width=", id= "Vd_main"/>
<mx:textinput x= "y=" 224 "text=" TE "width=" 208 "id=" Txt_filename "/>
<mx:button x= "$" y= "224" label= "Record" id= "Btn_record"/>
<mx:button x= "327" y= "224" label= "Stop" id= "Btn_stop"/>
<mx:button x= "y=" label= "Replay" id= "Btn_replay"/>
<mx:label x= "y=" 288 "text=" status Hint: "width=" 362 "id=" Txt_status "/>

5. Import the package and define the variables as follows:
Import Mx.controls.Alert;

private Var netconnection:netconnection;
private Var Netstream:netstream;
private var appserver:string= "Rtmp://192.168.0.249/test_video1";
private Var Camera:camera;
private Var Microphone:microphone;

General workflow for audio streaming or video streaming: The following steps briefly describe the workflow for publishing live audio and video:

A. Create a Netconnection object.
B. Use the Netconnection.connect () method to connect to the application instance on the server.
C. Create a NetStream object to create the data flow in the connection.
D. Use the Netstream.attachaudio () method to capture the audio and send it through the stream, and then use the Netstream.attachcamera () method to capture and send the video.
E. Use the Netstream.publish () method to specify a unique name for the stream, and then send the data to the server through that stream so that other users can receive the data. You can also record when you publish data so that users can play back the data at a later time.

6.creationcomplete= "Init ()", page initialization code
Private Function init (): void
{
Netconnection = new Netconnection ();
Netconnection.connect (AppServer);
Netconnection.addeventlistener (Netstatusevent.net_status,netstatushandler);
Btn_record.addeventlistener (mouseevent.click,recordflv);
Btn_stop.addeventlistener (Mouseevent.click,stoprecord);
Btn_replay.addeventlistener (mouseevent.click,replayflv);

Initmedia ();
}

The code is simple, just doing some initialization work.

7. Implement Initmedia ()
Private Function Initmedia (): void
{
Camera = Camera.getcamera ();
Camera.setmode (300,180,15);
Camera.setquality (0,100);
Vd_main.attachcamera (camera);
Microphone = Microphone.getmicrophone ();
}

Code Description:
This code is actually initializing the video device and the audio device

The camera class captures video from the camera that is connected to the computer running Flash Player.
Important: Flash Player Displays the Privacy dialog box, from which the user can choose whether to allow or deny access to the camera. Ensure that the application window size is at least 215 x 138 pixels, which is the minimum size required to display the dialog box.

To create or reference a Camera object, use the Getcamera () method.

The Camera.setmode () method is to set the capture mode of the camera to the native mode that best meets the specified requirements
1th parameter: The capture width of the request, in pixels. The default value is 160
2nd parameter: The requested capture height, in pixels. The default value is 120.
The 3rd parameter: the request rate that the camera captures data should use in frames per second. The default value is 15.

The Camera.setquality () method sets the maximum bandwidth per second or the required screen quality for the current output video input signal
1th parameter: Specifies the maximum bandwidth that the current output video input signal can use, in bytes per second. To specify that Flash Player video can use the required amount of bandwidth to maintain the value of quality, pass 0 for bandwidth. The default value is 16384.
The 2nd parameter: An integer that specifies the desired screen quality level, determined by the amount of compression applied to each video frame. Acceptable values range from 1 (lowest quality, maximum compression) to 100 (highest quality, no compression). To specify that the screen quality can be changed to avoid exceeding the bandwidth as needed, pass 0 to quality.

Vd_main.attachcamera (camera) is the display of a video stream from the camera to the control

The microphone class captures audio from a microphone that is connected to a computer running Flash Player
Important: Flash Player Displays the Privacy dialog box, from which the user can choose whether to allow or deny access to the microphone. Make sure that the application window is at least 215 x 138 pixels, which is the minimum size required to display the dialog box.

To create or reference a microphone object, use the Microphone.getmicrophone () method.

8. Implement Netstatushandler
Private Function Netstatushandler (evt:netstatusevent): void
{
if (evt.info.code== "NetConnection.Connect.Success")
{
txt_status.text= "FMS connection Success";
NetStream = new NetStream (netconnection);
}
Else
{
Alert.show ("FMS connection Failed");
}
}

9. Implementing the Record button event
Private Function recordflv (evt:mouseevent): void
{
txt_status.text= "Start recording";
Netstream.attachcamera (camera);
Netstream.attachaudio (microphone);
Netstream.publish (Txt_filename.text, "record");
}

Code Description:
Publish () Method: sends an audio stream, a video stream, and a text message stream from the client to Flash Media Server, and optionally record the stream during transmission. This method is intended for use only by publishers of the specified stream.
1th parameter: A string that identifies the stream.
2nd parameter: A string that specifies how to publish the stream. Valid values are "record", "Append", and "live". The default value is "Live". (these 3 parameters differ as follows:)
If you pass a record, Flash Player publishes and records real-time data, while saving the recorded data to a new file with a name that matches the value passed to the name parameter. This file is saved in a subdirectory of the directory where the server application resides on the server. If the file exists, the file is overwritten.
If you pass "append", Flash Player publishes and records real-time data and attaches the recorded data to a file that matches the value that is passed to the name parameter, which is stored in a subdirectory of the directory on the server that contains the server application. If a 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 live data without recording. If there is a file with a name that matches the value passed to the name parameter, delete it.

10. Stop Event
Private Function Stoprecord (evt:mouseevent): void
{
txt_status.text= "Stop Recording";
Netstream.close ();
}

11. Playback Events
Private Function replayflv (evt:mouseevent): void
{
txt_status.text= "Start playback";
vd_main.source=appserver+ "/" +txt_filename.text;
Vd_main.play ();
}

12. Running the program


13. Code Download
Http://files.cnblogs.com/aierong/Video1.rar
After you receive the code, please return to the article under the message to tell it! If you don't get it, I can send it again!

Provide code to learn from each other and explore together! Please communicate with us more!
1. If you have any questions about the code, you can leave a message in the comment section of the article, I will do my best to answer you!
2. If you find a bug in the process of running the code, or if you have any good suggestions and comments, you can also leave a message in the comment section of the article, I will correct it in time!


Comment Area Use tips:
Comment Area message (using advanced review) is can be affixed to the picture, if there are difficult to describe the problem, you can paste pictures and text together to explain
Thank you!

Collection and sharing

Bookmark Add to Baidu add to Yahoo Collection

RSS Subscribe to myWhat is RSS?




Dongguan. NET Club

You are welcome to join

Fms3 and Flex to create online video recording and playback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.