Fms3 and flex create online video recording and Playback

Source: Internet
Author: User
Flex, fms3 SeriesArticleNavigation
  1. 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. For simple fms3 and flex applications, refer to my previous article.
Http://www.cnblogs.com/aierong/archive/2009/01/09/flex_fms_chat.html

 

2. Preparations
Create a new folder test_video1 in the application under the installation directory of the FMS, and then start the FMS server.

 

3. Open fb3 and create a project

 

4. Drag the control in the main mxml file. The interface is as follows:

DetailsCodeAs follows:
<Mx: videodisplay x = "34" Y = "10" Height = "200" width = "360" id = "vd_main"/>
<Mx: textinput x = "34" Y = "224" text = "te" width = "208" id = "txt_filename"/>
<Mx: button x = "250" Y = "224" label = "record" id = "btn_record"/>
<Mx: button x = "327" Y = "224" label = "stop" id = "btn_stop"/>
<Mx: button x = "250" Y = "256" label = "replay" id = "btn_replay"/>
<Mx: Label x = "34" Y = "288" text = "status prompt:" 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;

The following describes how to publish real-time audio and video streams:

A. Create a netconnection object.
B. Use netconnection. Connect () to connect to the application on the serverProgramInstance.
C. Create a netstream object to create a data stream in the connection.
D. Use the netstream. attachaudio () method to capture the audio and send it through the stream. 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 the stream so that other users can receive the data. You can also record data when publishing so that you can play back the data later.

 

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 relatively simple, that is, 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 used to initialize the video device and audio device.

The camera class can capture videos from cameras connected to computers running Flash Player.
Important: the "privacy" dialog box is displayed for flash player. You can choose whether to allow or deny access to the camera. Make sure that the application window size is at least 215x138 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 camera capture mode to the local mode that best meets the specified requirements.
1st parameters: Request capture width, in pixels. The default value is 160.
2nd parameters: Request capture height, in pixels. The default value is 120.
3rd parameters: the request rate used by the camera to capture data, in the unit of frames per second. The default value is 15.

The camera. setquality () method sets the maximum bandwidth per second or the image quality required for the input signal of the current output video.
1st parameters: specify the maximum bandwidth available for the current output video input signal, in bytes per second. To specify the amount of bandwidth required for a Flash Player video to maintain the quality value, pass 0 to the bandwidth. The default value is 16384.
2nd parameters: an integer that specifies the image quality level. This level is determined by the amount of compression applied to each video frame. The range of acceptable values is 1 (minimum quality, maximum compression) to 100 (highest quality, no compression ). To specify the image quality, you can change the image quality as needed to avoid exceeding the bandwidth. Pass 0 to quality.

Vd_main.attachcamera (CAMERA) is to display the video stream from the camera to this control.

The microphone class can capture audio from a microphone connected to a computer running Flash Player.
Important: the "privacy" dialog box is displayed for flash player. You can choose whether to allow or deny access to the microphone. Make sure that the application window size is at least 215x138 pixels, which is the minimum size required for the display 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 = "milliseconds connected ";
Netstream = new netstream (netconnection );
}
Else
{
Alert. Show ("failed to connect to FMS ");
}
}

 

9. Implement recording button events
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: 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. The file is saved in the subdirectory of the server application 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.

 

10. Stop the event
Private function stoprecord (EVT: mouseevent): void
{
Txt_status.text = "stop recording ";
Netstream. Close ();
}

 

11. Replay events
Private function replayflv (EVT: mouseevent): void
{
Txt_status.text = "start playback ";
Vd_main.source = appserver + "/" + txt_filename.text;
Vd_main.play ();
}

 

12. Run the program

 

13. Download Code
Http://files.cnblogs.com/aierong/Video1.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 ()
{
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

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.