Flash/flex learning notes (6): Create a FLV Video Player Based on XML data sources

Source: Internet
Author: User

After a long time of hard work today, we finally developed a simple video player, which can be used to communicate with company leaders.

Steps:

1. Drag a "flvplayback" component to the stage in Flash cs4.

Note: flvplayback has basic FLV playback functions. You can simply set the attributes to play the video.

2. Load the XML Data Source

The XML data source format is as follows:

 
<? XML version = "1.0" encoding = "UTF-8"?> <DATA> <item FLV = "FLV address 1" photo = "thumbnail 1" url = "Click Path 1" name = "name 1"> </item> <item FLV = "FLV address 2" photo = "thumbnail 2" url = "Click Path 2" name = "name 2"> </item>... </data>

 

Core of loading XML by using ActionScriptCode:

VaR _ xml: XML; // declare an XML-type variable, used to receive data VaR _ urlreq: URLRequest = new URLRequest (); _ urlreq. url = "XML file address"; VaR _ Loader: urlloader = new urlloader (_ urlreq); // (asynchronous) Start loading xml_loader.addeventlistener (event. complete, loadcompleted); // configure the callback function after loading. // The processing function loadcompleted (E: Event): void {_ xml = XML (_ loader. data); For each (VAR item in _ xml. ITEM) {trace (item. @ FLV + "," + item. @ photo + "," + item. @ URL + "," + item. @ name}); // use @ in as to access the attributes of XML node elements }}

 

3. Bind The tilelist component with XML data

Tilelist is a bit similar to wrappanel in Silverlight. In "Flash/flex Study Notes (2): capture camera", the following list is the application of the tilelist component. The usage of tilelist is not repeated.

As for binding tilelist to XML, dataprovider is mainly used to complete

Key as3Source code:

Import FL. events. listevent; import FL. video. videoevent; import FL. video. videoprogressevent; lbldebug. visible = false; // disable debugging flv1.play (); // by default, the first VaR _ xml: XML; VaR _ urlreq: URLRequest = new URLRequest (); _ urlreq. url = "XML file address"; VaR _ Loader: urlloader = new urlloader (_ urlreq); _ loader. addeventlistener (event. complete, loadcompleted); VaR _ DP: dataprovider = new dataprovider (); VaR _ currentindex: uint = 0; function loadcompleted (E: Event ): void {_ xml = XML (_ loader. data); For each (VAR item in _ xml. ITEM) {_ DP. additem ({FLV: item. @ FLV, source: item. @ photo, URL: item. @ URL, name: item. @ name});} If (_ DP. length> 0) {playflv (_ currentindex); flv1.addeventlistener (videoevent. complete, playcompleted); flv1.addeventlistener (videoevent. buffering_state_entered, buffered); flv1.addeventlistener (videoevent. stopped_state_entered, stopentered); flv1.addeventlistener (videoevent. paused_state_entered, pauseentered); flv1.addeventlistener (videoevent. playing_state_entered, playingentered); flv1.addeventlistener (videoprogressevent. progress, progresshandler) ;}} lst1.dataprovider = _ DP; lst1.addeventlistener (listevent. item_click, clickbaby); // when you click baby, play the corresponding video function clickbaby (E: listevent) {_ currentindex = E. index; // trace (_ currentindex); playflv (_ currentindex);} // after playback is complete, insert a function playcompleted (E: videoevent) {trace ("playback completed: "+ E); playflv (_ currentindex + 1);} function buffered (E: videoevent) {/* trace (" buffering... "+ E); lbldebug. TEXT = "buffering... "; */flv1.playwhenenoughdownloaded ();} function stopentered (E: videoevent) {// trace (" stopped... "); // lbldebug. TEXT = "stopped... ";} function pauseentered (E: videoevent) {// trace (" paused... "); // lbldebug. TEXT = "paused... "; pause1.visible = true;} function playingentered (E: videoevent) {// trace (" playing... "); // lbldebug. TEXT = "playing... "; pause1.visible = false;} function progresshandler (E: videoprogressevent) {// trace (E); // flv1.playwhenenoughdownloaded (); // trace (" buffer time: "+ flv1.buffertime); // trace (" flv1.buffering = "+ flv1.buffering +", flv1.paused = "+ flv1.paused) if (flv1.buffering | flv1.paused) {// This is a problem, this will invalidate the user's click pause (no automatic pause for loading the buffer due to slow network speed is found yet) flv1.playwhenenoughdownloaded ();}} // play the video function playflv (flvindex: uint) {// trace ("flvindex =" + flvindex + ", _ currentindex =" + _ currentindex ); if (flvindex> = _ DP. length) {flvindex = 0;} var item: Object = _ DP. getitemat (flvindex); flv1.load (item. FLV); flv1.play (); pause1.visible = false; _ currentindex = flvindex; lst1.scrolltoindex (_ currentindex);} STOP ();

Online demonstration:

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.