Skillfully using the media components of Flash to achieve multiple MP3 Lianbo

Source: Internet
Author: User
Tags array definition

The media component of the Flash MX 2004 is really good, when you use it you may find that the Mediadisplay component can play only one mp3, and if you want to play a song and then play another song, you can't. Today to introduce you how to use Mediacontroller and mediadisplay components to achieve a number of MP3 network.

The player interface is shown in figure:

Ideas are as follows:

The song list is recorded with an external XML file for easy modification. Start by defining an XML object, loading the external XML file, loading it, and then extracting the list of songs into an array. Then use the Attachmovie () method to load the Mediacontroller and Mediadisplay components into the stage, then set their own parameters and associate them (Media.associatecontroller () or the Media.associatedisplay () method is ok) when a song is played, triggers the Mediadisplay complete event, In this event I unloaded the Mediacontroller and Mediadisplay components and reload the Mediacontroller and Mediadisplay components (using the Attachmovie () method) and gave a new instance name. and set the Mediadisplay component's playback content for the next song, so back and forth to play all the songs.

Production steps:

1. Create a new flash document, set the stage size: 220*70.

2. Drag components Mediacontroller and mediadisplay into the stage and remove them from the stage. The purpose of this is to load the two components into the library and prepare the Mediacontroller and Mediadisplay components to be loaded into the stage with the Attachmovie () method.

3. Create a new layer, named function. Enter the following as code on the first frame of the layer:

function init () {flagnum = 1;//play the song's Mark Mp3num = 0;attachcontroller ();};/ /load the Mediacontroller and Mediadisplay components into the stage and initialize the function Attachcontroller () {///load the Mediadisplay component on the stage and give the definition instance name "Mp3container" +  Flagnum _root.attachmovie ("Mediadisplay", "Mp3container" + flagnum,2);   Set the component initial parameter with (_root["Mp3container" + Flagnum]) {_x = 250;   _y = 0; Setmedia (Myarray[mp3num], "MP3");} Load the Mediacontroller component into the stage and give the definition instance name "Mp3controller" + Flagnum_root.attachmovie ("Mediacontroller", "Mp3controller" +    flagnum,1)//Set the component initial parameter with (_root["Mp3controller" + Flagnum]) {_x = 0;   _y = 0;   Activeplaycontrol = false;   Controllerpolicy = "on";  Associate the Mediacontroller and Mediadisplay components Associatedisplay (_root["Mp3container" + flagnum]); };};    function PlayNext () {//Remove Mediacontroller and Mediadisplay components from the stage _root["Mp3controller" + flagnum].removemovieclip ();    _root["Mp3container" + flagnum].removemovieclip ();    Flagnum + 1;    Mp3num + 1;    If played to the last song, will mp3num = 0, meaning will start from the beginning of the IF (Mp3num = = mp3total) mp3num = 0; ReloadMediacontroller and Mediadisplay components, and give a new instance name Attachcontroller (); Because the Mediacontroller and Mediadisplay components are unloaded from the stage after each play, and then reload the Mediacontroller and Mediadisplay components and give another instance name//So you want to go back to the note The listeners of the book Broadcast things _root["Mp3container" + flagnum].addeventlistener ("complete", MyListener);

4. Create a new layer, name action, and enter the as code in the first frame of the layer:

The Stop ();//array myarray is used to store the song names in the loaded XML document var myarray = new Array ();//Total number of songs var mp3total:number;var myxml = new XML (); Myxml.ignorewhite = True;myxml.load ("Mp3list.xml"); myxml.onload = function () {var e = myXml.firstChild.childNodes; Mp3total = E.length;for (var i = 0; i<mp3total; i++) {Myarray.push (e[i].attributes["title"]); Init (); _root["Mp3container" +flagnum].addeventlistener ("complete", MyListener); var mylistener = new Object (); mylistener.complete = function (eventobject) {PlayNext ();};

5. Save the file, Ctrl+enter test the movie and output the SWF file. When you test a movie, you get an error, not to worry, because the XML document is not generated.

6. Create an XML document:

<?xml version= "1.0" encoding= "Utf-8"?><mp3lists> <item title=  "1.mp3"/> <item title= "  2.mp3 "/></mp3lists>

Save the file in the same folder as the original SWF file, named Mp3list.xml.

7. In the original file directory into two mp3, the default name is 1.mp3,2.mp3, here the number of names and songs you can modify the XML file.

So far, a two-MP3 player can be used, open the SWF file, you will hear beautiful music, and see the playback progress show MP3 player often function. Note: In order for this player to be used on your site, you must upload the SWF file and XML and two MP3 files to the same folder on your site.

  Click here to download the source file



Related Article

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.