Need to randomize MP3 player
T_Townrampage Sep 16, 2010 10:32 AMHello guys.
I have a simple mp3 player in AS2 and I need a way to randomize the songs.
Here is my AS2 code.
__________________________________________________________________________________________ ____________________________________
var s:Sound = new Sound();
s.onSoundComplete = playTracks;
s.setVolume(75);
var sa:Array = new Array();
var cps:Number = -1;
var pos:Number;
////load the xml data for the mp3 tracks
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 list(nodes[i].attributes.url, nodes[i].attributes.artist, nodes[i].attributes.track));
}
playTracks();
}
/////your xml file is called here, if you change the name of your xml file also change it here
xml.load("tracks.xml");
///function for playing your tracks, note that the volume is currently set to 75, 100 =max, 0=mute
function playTracks():Void
{
s = new Sound();
s.onSoundComplete = playTracks;
s.setVolume(75);
mute.gotoAndStop("on");
if(cps == sa.length - 1)
{
cps = 0;
s.loadSound(sa[cps].earl, true);
}
else
{
s.loadSound(sa[++cps].earl, true);
}
info.info2.trackInfo.text = sa[cps].track + " " ;
info.info2.trackInfo2.text = sa[cps].artist + " " ;
onOff.gotoAndStop("pause");
textPos = 0;
}
///function for playing the previous track
function playPrevious():Void
{
s = new Sound();
s.onSoundComplete = playTracks;
s.setVolume(75);
mute.gotoAndStop("on");
if(cps == 0)
{
cps = sa.length-1;
s.loadSound(sa[cps].earl, true);
}
else
{
s.loadSound(sa[--cps].earl, true);
}
info.info2.trackInfo.text = sa[cps].track + " " ;
info.info2.trackInfo2.text = sa[cps].artist + " " ;
onOff.gotoAndStop("pause");
}
///function pauses the music
function musicOff():Void
{
pos = s.position;
s.stop();
}
///function for continuing play
function musicOn():Void
{
s.start(pos/1000);
}
/////this determines rollover states depending on what position the on/off toggle is in,
//currently I'm not using rollover states for my buttons, but you can add them if you like.
onOff.onRollOver = function()
{
if(this._currentframe == 1) this.gotoAndStop("pauseOver");
else this.gotoAndStop("playOver");
}
/////this determines the postion of the pause/play toggle
onOff.onRollOut = onOff.onReleaseOutside = function()
{
if(this._currentframe == 10) this.gotoAndStop("pause");
else this.gotoAndStop("play");
}
////on release of the pause/play toggle this determines wether to pause or resume play
onOff.onRelease = function()
{
if(this._currentframe == 10)
{
this.gotoAndStop("playOver");
this._parent.musicOff();
alphaTween = new mx.transitions.Tween (equ, "_alpha",
mx.transitions.easing.Regular.easeOut, 100, 0, 0.75,
true);
}
else
{
this.gotoAndStop("pauseOver");
this._parent.musicOn();
alphaTween = new mx.transitions.Tween (equ, "_alpha",
mx.transitions.easing.Regular.easeOut, 0, 100, 0.75,
true);
}
}
////rollover states for the next button, again I'm not using any
next.onRollOver = function()
{
this.gotoAndStop("nextOver");
}
next.onRollOut = next.onReleaseOutside = function()
{
this.gotoAndStop("next");
}
///this determines wether the equalizer has an alpha of 0, if it does the equalizer will return to 100%
///when the next button is pressed
next.onRelease = function()
{
this._parent.playTracks();
if(equ._alpha == 0)
{
alphaTween = new mx.transitions.Tween (equ, "_alpha",
mx.transitions.easing.Regular.easeOut, 0, 100, 2,
true);
}
else
{
}
}
///this determines wether the equalizer has an alpha of 0, if it does the equalizer will return to 100%
///when the prev button is pressed
prev.onRelease = function() {
playPrevious();
if(equ._alpha == 0)
{
alphaTween = new mx.transitions.Tween (equ, "_alpha",
mx.transitions.easing.Regular.easeOut, 0, 100, 2,
true);
}
else
{
}
}
__________________________________________________________________________________________ __________________________________
Thanks for any help!!


