4 Replies Latest reply on Apr 4, 2007 7:53 AM by abeall

    Random MP3 Button

    Mike_Watt Level 2
      I want to know how to place a button on the screen that, when clicked, will play an mp3 file at random from those available within a specified directory... So, I'm thinking that these mp3 should be called up on the fly, and not be embedded into the actual SWF document... anyone have any tips for this? (I'm using Flash MX - and not all that great at it, either)....

      THANKS!
        • 1. Random MP3 Button
          abeall Level 3
          You could use PHP or another server-side language to scan the directory for all .mp3 files and returns XML, or you could create a static .xml file listing out all the MP3 files(which you update as needed), and either way you load the XML and load a random file. Something like this:

          XML:

          <audio>
          <track file="mysong1.mp3" />
          <track file="mysong2.mp3" />
          <track file="mysong3.mp3" />
          <track file="mysong4.mp3" />
          <track file="mysong5.mp3" />
          <track file="mysong6.mp3" />
          </audio>

          ActionScript:
          var myAudio = new Sound();
          var myXML = new XML();
          myXML.ignoreWhite = true;
          myXML.load('songlist.xml'); // or point it to a PHP file which returns XML
          myXML.onLoad = function(){
          var randomSong = myXML.firstChild.childNodes[random(myXML.firstChild.childNodes.length)];
          myAudio.loadSound(randomSong.attributes.file);
          }

          This is not tested, but that's the general idea.

          Good luck.
          • 2. Re: Random MP3 Button
            Mike_Watt Level 2
            This seems straight forward enough... but still, would you (or anyone) be willing to set this up for a fee? I will create the FLA with the GUI as needed and you (or someone) write the script (including php, etc.) that is needed to execute it? Then, all I'd need to know is how to update the file (if a static xml file)...

            Thoughts? I imagine this wouldn't be too much work for someone who knows what they're doing...

            I would also be willing to negotiate for ad placement, etc... I can't disclose the site right now, but it is going to be part of a small network supporting a national - and very well known - radio program with a target demo of Males 18-54. Anyway, details for anyone who is actually interested... (abeall - you have first dibs is you want them.... )

            I can be emailed at Skippy [at] nyc (dot) rr <dot> com.
            Thanks!
            • 3. Re: Random MP3 Button
              abeall Level 3
              Well, the script I posted up top is basically done, give it a shot. Just copy paste it into frame 1, change the 'file' paths in the XML to real files you have on your server, and make sure myXML.load is pointing to the file.
              • 4. Re: Random MP3 Button
                abeall Level 3
                Oh, you want it to work when you click a button. Try modifying the ActionScript on frame 1 to this:

                var myAudio = new Sound();
                var myXML = new XML();
                myXML.ignoreWhite = true;
                myXML.load('songlist.xml'); // or point it to a PHP file which returns XML

                function loadRandomSong(){
                var randomSong = myXML.firstChild.childNodes[random(myXML.firstChild.childNodes.length)];
                myAudio.loadSound(randomSong.attributes.file);
                }

                Then on the button put in the actions:
                on(release){
                loadRandomSong();
                }