As3 simple player [Author: qingren]

Source: Internet
Author: User
Tags flv file

[Knowledge point]
1. Construct the video netconnection and netstream;
2. Use soundtransform to control the volume. (This consumes some energy. In As2, setvolume can be used, but as3 won't work );
3. Pause pause () and Resume playback of paused video streams using resume ();
4. onmetadata is used to receive video information (for example, the total video time data. duration );
5. Use NetStatusEvent. NET_STATUS to start and end the video stream. If the value of info. code is "NetStream. Play. Stop", you can replay or Play a new video;
// The above is just some of the most basic video knowledge. I hope you will be able to get something from as3. this will be my greatest pleasure.
[Effect]
Video .swf
[STEP]
Step 1: Create the play button and pause button. The instance names are play_btn and pause_btn;
Step 2: draw two long Rectangles and convert them into video clips. The instance names are jzjd_mc and bfjd_mc, respectively, and serve as the loading and playing progress bars;
Step 3: Right-click the database and select "new video". In the displayed dialog box, click "video (subject to the control of ActionScript)" and drag it to the scenario. The instance name is vid;
Step 4: pull two dynamic texts in the scenario. The instance names are bftxt (display playback time) and zcdtxt (display total time );
Step 5: Draw a long rectangle and convert it into a video clip. As the volume bar, the Instance name is ylt_mc;
Step 6: Draw a small circle and convert it into a video clip. As the volume slider, the Instance name is ylhk_mc;
Step 7: Write the following code on the frame:
[Code]

 

Code

// Create a NetConnection object
Var nc: NetConnection = new NetConnection ();
/* If you connect to the FLV file that is not using the server, pass the value to the connect () method.
Null to play FLV files */
Nc. connect (null );
/* Create a NetStream object (this object uses the NetConnection object as a parameter) and
FLV file to be loaded */
Var ns: NetStream = new NetStream (nc );
Ns. play ("http://www.helpexamples.com/flash/video/cuepoints.flv ");
/* Use the attachNetStream () method of the Video class to append the previously created NetStream.
Object (the video Instance name is vid )*/
Vid. attachNetStream (ns );
// Initial volume value
Var yl: Number = 0.5;
Var nsyl: SoundTransform. = new SoundTransform ();
// Nsyl. volume = yl
// The video playback progress and loading progress are scaled to 0.
Bfjd_mc.scaleX = jzjd_mc.scaleX = 0;
// Declare that the initial value of the variable playback and download percentage and total time (in seconds) is 0.
Var bfbfb: int = 0;
Var xzbfb: int = 0;
Var _ duration: Number = 0;
// Specify the object on which the callback method is called
Var _ client: Object = new Object ();
_ Client. onMetaData = onMetaData;
Ns. client = _ client;
// Button visibility and adding listening events
Play_btn.visible = false;
Pause_btn.visible = true;
Pause_btn.addEventListener (MouseEvent. CLICK, zt );
Play_btn.addEventListener (MouseEvent. CLICK, bf );
// Ignore errors
Ns. addEventListener (AsyncErrorEvent. ASYNC_ERROR, asyncErrorHandler );
Function asyncErrorHandler (event: AsyncErrorEvent): void {
}
// Pause
Function zt (event: MouseEvent): void {
Play_btn.visible = true;
Pause_btn.visible = false;
// Pause the video
NS. Pause ();
}
// Play
Function BF (Event: mouseevent): void {
Play_btn.visible = false;
Pause_btn.visible = true;
// Resume paused video streams.
NS. Resume ();
Addeventlistener (event. enter_frame, GX );
}
// Scheduling when receiving descriptive information embedded in the FLV file being played
Function onmetadata (data: Object): void {
_ Duration = data. duration;
}
// Declare the variable playback Signal
VaR bfxh: string;
// Start and end of the listener video stream
NS. addeventlistener (netstatusevent. net_status, statushandler );
Function statushandler (Event: netstatusevent): void {
Bfxh = event.info. Code;
}
// Update Progress and text display continuously
Addeventlistener (event. enter_frame, GX );
Function GX (Event: Event): void {
If (ns. bytesLoaded> 0 ){
// Loading progress
Xzbfb = ns. bytesLoaded/ns. bytesTotal * 100;
Jzjd_mc.scaleX = xzbfb/100;
}
If (_ duration> 0 & ns. time> 0 ){
// Playback progress
Bfbfb = ns. time/_ duration * 100;
Bfjd_mc.scaleX = bfbfb/100;
}
If (bfxh = "NetStream. Play. Stop "){
// Set when playback is complete
Bfbfb = 0;
Bfjd_mc.scaleX = 0;
Ns. pause ();
Ns. seek (0); // place the playback header at the beginning of the video.
Play_btn.visible = true;
Pause_btn.visible = false;
}
// Text display content
Bftxt. text = Math. round (ns. time/60) + ":" + Math. round (ns. time % 60 );
Zcdtxt. text = Math. round (_ duration/60) + ":" + Math. round (_ duration % 60 );
// Volume control
Yl = (ylhk_mc.x-345)/50;
Ylt_mc.scalex = yl;
Nsyl. volume = yl;
NS. soundtransform. = nsyl;
}
// Volume slider Drag Control
VaR Fw: rectangle = new rectangle (345,328, 50, 0); // drag range
Ylhk_mc.addeventlistener (mouseevent. mouse_down, ylhkax );
Ylhk_mc.addeventlistener (mouseevent. mouse_up, ylhksk );
Stage. addeventlistener (mouseevent. mouse_up, ylhksk );
Function ylhkax (Event: mouseevent): void {
Ylhk_mc.startdrag (false, Fw );
}
Function ylhksk (Event: mouseevent): void {
Ylhk_mc.stopdrag ();
}

 

 

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.