Flex online video recording and playback (source code)

Source: Internet
Author: User


:
This article comes from the podcast http://xinsync.xju.edu.cn on the iceberg, the original address: http://xinsync.xju.edu.cn/index.php/archives/1703
Step 1: Install Flash Midea Server 2, create a test folder under Flash Midea Server 2 \ applications in the installation path (defined by the folder name), and restart the FMS.
Step 2: write the code. The following is the complete code:
Program code
<? Xml version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml" layout =" absolute "creationComplete =" playinit () "width =" 366 "height =" 350 ">
<Mx: Script>
<! [CDATA [
Import mx. events. SliderEvent;
Import mx. events. VideoEvent;
Import mx. collections. ArrayCollection;
Import mx. rpc. events. ResultEvent;
Import mx. core. UIComponent;
Import flash. events. StatusEvent;
Import flash. events. SecurityErrorEvent;
Import flash. media. Camera;
Import flash. media. Microphone;
Import flash.net. NetConnection;
// Because ff0 is used for fms and as3 in flex3 uses amf3. therefore, AFM0 must be used for flex.
NetConnection. defaultObjectEncoding = flash.net. ObjectEncoding. AMF0;
// Video server address
Private var _ videoServerURL: String = "rtmp: // 192.168.0.107/test ";
Private var _ camera: Camera; // defines a camera.
Private var _ mic: Microphone; // defines a Microphone.
Private var _ localVideo: Video; // defines a local Video.
Private var _ netConnection: NetConnection;
Private var _ outStream: NetStream; // defines an output stream.
Private var _ inStream: NetStream; // defines an input stream
Private var isplaying: Boolean = false; // you can specify whether the flag is being played.
Private var isrecing: Boolean = false; // you can specify whether the recording tag is being recorded.
Private var ispauseing: Boolean = false; // you can specify whether the flag is being paused.
Private var _ duration: Number; // defines the video duration.
Private var playPosition: Number; // you can specify the playback progress position.
Private var soundPosition: Number; // defines the position of the sound Size Control bar.
Private function playinit (): void {
T_hs_control.enabled = false;
T_btn_play.enabled = false;
T_btn_stop.enabled = false;
T_btn_rec.enabled = false;
T_btn_save.enabled = false;
T_lbl_rec.visible = false;
InitCameraAndMic (); // initialize the camera
}
// Initialize the camera
// Determine whether a camera or access permission exists
Private function initCameraAndMic (): void
{
_ Camera = Camera. getCamera ();
If (_ camera! = Null)
{
_ Camera. addEventListener (StatusEvent. STATUS ,__ onStatusHandler );
_ Camera. setMode (320,420, 30 );
// T_flv_video.attachCamera (_ camera );
_ LocalVideo = new Video ();
_ LocalVideo. width = 320;
_ Localvideo. Height = 240;
_ Localvideo. attachcamera (_ camera );
T_flv_video.addchild (_ localvideo );
}
_ MIC = microphone. getmicrophone ();
If (_ mic! = NULL)
{
// No listening mic connection status is added
// Set the audio transmitted from the local microphone to the local system speaker
/*
_ Mic. setuseechosuppression (true );
_ Mic. setloopback (true );
*/
_ Mic. setsilencelevel (0,-1); // sets the microphone to remain active and continuously receives audio data from the set.
_ Mic. Gain = 80; // set the microphone sound size
}
}
// Start recording the video
// Check the network connection status
Private function beginorshowrecvideo (): void
{
_ Netconnection = new netconnection ();
_ Netconnection. addeventlistener (netstatusevent. net_status ,__ onnetstatushandler );
_ NetConnection. addEventListener (SecurityErrorEvent. SECURITY_ERROR ,__ onSecurityErrorHandler );
_ NetConnection. connect (_ videoServerURL );
}
// Record the video and transmit the video and audio stream to the server.
Private function beginRecConnectStream (): void
{
If (_ localVideo! = Null)
{
_ LocalVideo. clear ();
T_flv_video.removeChild (_ localVideo );
_ LocalVideo = new Video ();
_ LocalVideo. width = 320;
_ LocalVideo. height = 240;
_ LocalVideo. attachCamera (_ camera );
T_flv_video.addChild (_ localVideo );
}
_ OutStream = new NetStream (_ netConnection );
_ OutStream. attachCamera (_ camera );
_ OutStream. attachAudio (_ mic );
_ OutStream. publish ("testVideo", "record ");
}
// Play the video
Private function showRecConnectStream (): void
{
_ InStream = new NetStream (_ netConnection );
_ InStream. addEventListener (NetStatusEvent. NET_STATUS ,__ onNetStatusHandler );
_ InStream. addEventListener (AsyncErrorEvent. ASYNC_ERROR ,__ onStreamErrorHandler );
// Define onMetaData to obtain video-related data
Var customClient: Object = new Object ();
CustomClient. onMetaData = function (metadata: Object): void
{
_ Duration = metadata. duration; // gets the video duration.
T_hs_control.maximum = _ duration; // sets the maximum value of the playback progress bar.
}
_ InStream. client = customClient;
// Delete the original _ localVideo to switch between recording and Playing videos.
_ LocalVideo. clear ();
T_flv_video.removeChild (_ localVideo );
_ LocalVideo = new Video ();
_ LocalVideo. width = 320;
_ LocalVideo. height = 240;
_ LocalVideo. attachNetStream (_ inStream );
_ InStream. play ("testVideo ");
T_flv_video.addChild (_ localVideo );
}
// Event after the play BUTTON is clicked: play the video, and analyze whether to play the video to adjust the tag on the BUTTON, which is displayed as playing or paused;
// Listen to the player
Private function flvplay (event: Event): void {
T_hs_control.enabled = true;
T_btn_stop.enabled = true;
T_btn_rec.enabled = false;
If (! Isplaying)
{
Isplaying = true;
BeginOrShowRecVideo ();
}
Else
{
_ InStream. togglePause (); // automatically switches between stop and playback.
}
If (isplaying)
{
If (ispauseing ){
T_btn_play.label = "play"
} Else {
T_btn_play.label = "Suspend"
}
Ispauseing =! Ispauseing;
}
AddEventListener (Event. ENTER_FRAME ,__ onEnterFrame );
}
// The stop button and video playback are complete.
// Reset some value variables and button enabled values.
Private function resetSomeParam (): void
{
_ InStream. close ();
T_btn_play.label = "play ";
T_lbl_playtime.text = "0: 00/" + formatTimes (_ duration );
T_hs_control.value = 0;
Isplaying = false;
Ispauseing = false;
T_hs_control.enabled = false;
T_btn_rec.enabled = true;
T_btn_stop.enabled = false;
}
// Click the stop play BUTTON to stop the video and adjust the tag on the corresponding BUTTON.
Private function flvStop (event: Event): void
{
ResetSomeParam ();
RemoveEventListener (Event. ENTER_FRAME ,__ onEnterFrame );
}
// Pull the progress bar
Private function thumbPress (event: SliderEvent): void {
_ InStream. togglePause ();
RemoveEventListener (Event. ENTER_FRAME ,__ onEnterFrame );
}
// After the progress bar changes, assign the value PLAYPOSITION;
Private function thumbChanges (event: SliderEvent): void {
PlayPosition = t_hs_control.value;
}
// Release the progress bar and send the PLAYPOSITION value to the player;
Private function thumbRelease (event: SliderEvent): void {
_ InStream. seek (playPosition );
_ InStream. togglePause ();
AddEventListener (Event. ENTER_FRAME ,__ onEnterFrame );
}
// Sound volume control
Private function sound_thumbChanges (event: SliderEvent): void {
SoundPosition = hs_sound.value;
}
Private function sound_thumbRelease (event: SliderEvent): void {
T_flv_video.volume = soundPosition;
}
// Format the time
Private function formatTimes (value: int): String {
Var result: String = (value % 60). toString ();
If (result. length = 1 ){
Result = Math. floor (value/60). toString () + ": 0" + result;
} Else {
Result = Math. floor (value/60). toString () + ":" + result;
}
Return result;
}
// Event after the recording BUTTON is clicked: record the video, and analyze whether to play the video to adjust the tag on the BUTTON to start or stop recording;
// Listen to the player
Private function recVideo (event: MouseEvent): void
{
If (! Isrecing) // start recording
{
Isrecing = true;
T_btn_rec.label = "stop recording ";
T_btn_play.enabled = false;
T_btn_save.enabled = false;
T_lbl_rec.visible = true;
BeginOrShowRecVideo ();
}
Else // stop recording
{
Isrecing = false;
T_btn_rec.label = "Start recording ";
T_btn_play.enabled = true;
T_btn_save.enabled = true;
T_lbl_rec.visible = false;
_ Outstream. Close ();
}
}
// Detects camera permission events
Private function _ onstatushandler (Event: statusevent): void
{
If (! _ Camera. muted)
{
T_btn_rec.enabled = true;
}
Else
{
Trace ("error: unable to link to the active camera !")
}
_ Camera. removeeventlistener (statusevent. Status ,__ onstatushandler );
}
// Network link event
// If the network connection is successful, start recording or watching the video
Private function _ onnetstatushandler (Event: netstatusevent): void
{
Switch (event.info. Code)
{
Case "NetConnection. Connect. Success ":
If (isrecing)
{
BeginRecConnectStream ();
}
Else
{
ShowRecConnectStream ();
}
Break;
Case "NetConnection. Connect. Failed ":
Trace ("connection failed !");
Break;
Case "NetStream. Play. StreamNotFound ":
Trace ("Stream not found:" + event );
Break;
}
}
Private function _ onSecurityErrorHandler (event: SecurityErrorEvent): void
{
Trace ("securityErrorHandler:" + event );
}
Private function _ onStreamErrorHandler (event: AsyncErrorEvent): void
{
Trace (event. error. message );
}
// Play video real-time events
// Change the playback progress bar value and playback time value in real time. When the video playback is complete, delete the real-time listening event and reset some initial values.
Private function _ onEnterFrame (event: Event): void
{
If (_ duration> 0 & _ inStream. time> 0)
{
T_hs_control.value = _ inStream. time;
T_lbl_playtime.text = formatTimes (_ inStream. time) + "/" + formatTimes (_ duration );
}
If (_ inStream. time = _ duration)
{
RemoveEventListener (Event. ENTER_FRAME ,__ onEnterFrame );
ResetSomeParam ();
}
}
]>
</Mx: Script>
<! -Analyze XML through HTTPSERVICE and obtain the RESULT. The RESULT is fed back to the SCRIPT.->
<! -Read XML extension content
<Mx: HTTPService id = "videoserver" url = "assets/videos. xml" result = "readXml (event)"/>
->
<! -Set the ID of the main video playing window to flvvideo. This is very important. You can choose other coordinates as you like.->
<Mx: panel X = "12" Y = "10" width = "342" Height = "282" layout = "absolute">
<Mx: videodisplay id = "t_flv_video" x = "1" Y = "1" width = "320" Height = "240"/>
<Mx: Label x = "243.5" Y = "6" text = "recording ..." Id = "t_lbl_rec" color = "#666666" fontsize = "12"/>
</MX: Panel>
<! -The playback progress bar of the player. Use the hslider provided by Flex to display the playback progress. You can also drag the video.->
<Mx: hslider id = "t_hs_control" x = "12" y = "296" minimum = "0 ″
Thumbpress = "thumbpress (event )"
Thumbrelease = "thumbrelease (event )"
Change = "thumbchanges (event)"/>
<! -Player sound control->
<Mx: hslider id = "hs_sound" x = "260" Y = "295" width = "80 ″
Minimum = "0" Maximum = "1 ″
Thumbrelease = "sound_thumbrelease (event )"
Change = "sound_thumbchanges (event )"
Value = "{t_flv_video.volume}"/>
<! -The play button is displayed as follows: Play or pause->
<Mx: Button id = "t_btn_play" x = "22" y = "320" click = "flvplay (event)" label = "play" fontSize = "12"/>
<! -Play button to stop playing a video.->
<Mx: Button id = "t_btn_stop" label = "stop" x = "85" y = "320 ″
Click = "flvStop (event)" fontSize = "12" enabled = "true"/>
<! -Time display->
<Mx: Label x = "170" y = "300" id = "t_lbl_playtime"
Text = "0: 00/0: 00 ″
Color = "# ffffff"/>
<! -The recording button is displayed as follows: Start recording or stop recording.
<Mx: Button x = "210" y = "320" label = "Start recording" click = "recVideo (event)" fontSize = "12" id = "t_btn_rec"/>
<! -Save the video button.->
<Mx: Button x = "299" y = "320" label = "save" fontSize = "12" id = "t_btn_save" enabled = "true"/>
</Mx: Application>
The above code has many shortcomings. Currently, the volume control does not take effect during playback. please correct me more. Thank you.
This article comes from the podcast http://xinsync.xju.edu.cn on the iceberg, the original address: http://xinsync.xju.edu.cn/index.php/archives/1703


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.