7 Replies Latest reply on Sep 26, 2009 10:27 AM by Ned Murphy

    randomize mp3 player

    andib311

      Hi

       

      I'm using this code I obtained from a tutorial to run an mp3 player that reads an .xml playlist. Could someone please tell me what I'd need to add/change to the code so that it plays the tracks in a random order each time the player loads - I'm not concerned about omitting repeats. I've tried adding a "random" command to the Array that holds the tracks but this didn't work.

       

      Thanks

       

      //Setup sound object
      var s:Sound=new Sound();
      s.onSoundComplete=playSong;
      s.setVolume(75);


      //Array of songs
      var sa:Array = new Array();


      //Currently playing song
      var cps:Number=-1;

      //Position of music
      var pos:Number;

      //Load the songs XML
      var xml:XML = new XML();
      xml.ignoreWhite = true;
      xml.onLoad = function()
      {
      var nodes:Array = this.firstChild.childNodes;
      for(var i=0;i<nodes.length;i++)
      {
      sa.push(new Song(nodes[i].attributes.url,nodes[i].attributes.artist,nodes[i].attributes.track));

      }
      playSong();
      }
      xml.load("/playlist.xml");

      //Play the MP3 File
      function playSong():Void
      {

      s=new Sound();
      s.onSoundComplete=playSong;
      if(cps==sa.length-1)
      {cps=0;
      s.loadSound(sa[cps].earl,true);
      }
      else
      {
      s.loadSound(sa[++cps].earl, true);
      }
      trackInfo.text=sa[cps].artist+' - '+sa[cps].track;
      playPause.gotoAndStop('pause');
      }

      //Pause the music
      function pauseIt():Void
      {
      pos=s.position;
      s.stop();
      }

      //Unpause the music
      function unPauseIt():Void
      {
      s.start(pos/1000);
      }

      //Music Controls

      //PlayPause toggle
      playPause.onRollOver=function()
      {
      if(this._currentframe==1)this.gotoAndStop('pauseOver');
      else this.gotoAndStop('playOver');
      }

      playPause.onRollOut=playPause.onReleaseOutside=function()
      {
      if(this._currentframe==10)this.gotoAndStop('pause');
      else this.gotoAndStop('play');
      }

      playPause.onRelease=function()
      {
      if(this._currentframe==10)
      {
        this.gotoAndStop('playOver');
        this._parent.pauseIt();
      }
      else
      {
        this.gotoAndStop('pauseOver');
        this._parent.unPauseIt();
      }
      }

      //Next Button
      next.onRollOver=function()
      {
      this.gotoAndStop('nextOver');
      }
      next.onRollOut=next.onReleaseOutside=function()
      {
      this.gotoAndStop('next');
      }
      next.onRelease=function()
      {
      this._parent.playSong();
      }

        • 1. Re: randomize mp3 player
          Ned Murphy Adobe Community Professional & MVP

          What you can do is shuffle the array once it has been assembled.  Hve a call to a separate shuffling function just after you push all the elements onto the array...

           

          // your line....

          sa.push(new Song(nodes[i].attributes.url,nodes[i].attributes.artist,nodes[i].attributes.tra ck));

          // shuffle the array

          sa = shuffle(sa);

           

          // and the function....

           

          function shuffle(a) {
          var len = a.length-1;
          for (i=len; i>=0; i--) {
                 var p = Math.floor(Math.random()*(i+1));
                 var t = a[i];
                 a[i] = a[p];
                 a[p] = t;
          }
          return a;
          }

          • 2. Re: randomize mp3 player
            andib311 Level 1

            Ok, here is what I've added in bold (section shows the //Array to //Play mp3 portion of the whole code)

             

            //Array of songs
            var sa:Array = new Array();


            //Currently playing song
            var cps:Number=-1;

            //Position of music
            var pos:Number;

            //Load the songs XML
            var xml:XML = new XML();
            xml.ignoreWhite = true;
            xml.onLoad = function()
            {
            var nodes:Array = this.firstChild.childNodes;
            for(var i=0;i<nodes.length;i++)
            {
            sa.push(new Song(nodes[i].attributes.url,nodes[i].attributes.artist,nodes[i].attributes.track));

            // shuffle the array
            sa = shuffle(sa);

            }
            playSong();
            }
            xml.load("/playlist.xml");


            // and the function....
            function shuffle(a) {
            var len = a.length-1;
            for (i=len; i>=0; i--) {
                   var p = Math.floor(Math.random()*(i+1));
                   var t = a[i];
                   a[i] = a[p];
                   a[p] = t;

            }
            return a;
            }
            //Play the MP3 File
            function playSong():Void
            {

            s=new Sound();
            s.onSoundComplete=playSong;
            if(cps==sa.length-1)
            {cps=0;
            s.loadSound(sa[cps].earl,true);
            }
            else
            {
            s.loadSound(sa[++cps].earl, true);
            }
            trackInfo.text=sa[cps].artist+' - '+sa[cps].track;
            playPause.gotoAndStop('pause');
            }

             

             

            These additions seem to have made no effect - the player continues to play the tracks in the order as they appear in the xml file. I've tried altering the lines

            // shuffle the array
            sa = shuffle(sa);

             

            to

             

            // shuffle the array
            sa = shuffle(a);

             

            as if I'm understanding it correctly, the function is defined as function shuffle(a) but this also does not work. Have I missed something? I appreciate your help

            • 3. Re: randomize mp3 player
              Ned Murphy Adobe Community Professional & MVP

              The call to the shuffle function should be outside the loop...  What you should do is place a trace before and after the call to the shuffle function...

               

              trace(sa);

               

              sa = shuffle(sa);

               

              trace(sa);

               

               

              to confirm whether or not the array has been shuffled.  Hopefully I didn't mess up translating it from what would have been a more AS3 compatible version.

              • 4. Re: randomize mp3 player
                andib311 Level 1

                Sorry but I still seem to be having difficulty with this. The Array just doesn't want to shuffle.

                I moved both the function and call to it after the command to load the xml.playlist.

                I've moved the function and call around a few other points in the code too but all to no avail...

                • 5. Re: randomize mp3 player
                  Ned Murphy Adobe Community Professional & MVP

                  I can't be sure where you are with the code at the moment, but...

                   

                  xml.onLoad = function()
                  {
                       var nodes:Array = this.firstChild.childNodes;
                       for(var i=0;i<nodes.length;i++)
                       {
                            sa.push(new Song(nodes[i].attributes.url,nodes[i].attributes.artist,nodes[i].attributes.tra ck));

                       }

                   

                       trace(sa); // before shuffle

                   

                       // shuffle the array after it is created

                       sa = shuffle(sa);

                   

                       trace(sa); // after shuffle

                   

                       playSong();
                  }

                  • 6. Re: randomize mp3 player
                    andib311 Level 1

                    Thanks for all your help Ned.

                     

                    Your last message matches how I had set out my code, and still it did not work. THEN, when uploading the site to the server via Dreamweaver's FTP, I selected yes rather than no to "upload independant files" along with all the pages and .as files, and now the mp3 player does seem to play a different track upon load. I'm not sure why this has had an affect - I usually select no so that I don't have to sit by and watch all the .swf and .flv files that link from the pages upload as well.

                     

                    So I guess it's all working now....

                     

                    Thanks again!

                    • 7. Re: randomize mp3 player
                      Ned Murphy Adobe Community Professional & MVP

                      You're welcome