[ActionScript 2.0] Music Randomization?
im trying to make it so that each time the player is loaded, a new song is played.
Code:
// 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("http://codingsource.no-ip.org/player/songs.php");
// Play the MP3 File
function playSong():Void
{
s = new Sound();
s.onSoundComplete = playSong;
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);
}
trackInfo.text = sa[cps].artist + " - " + sa[cps].track;
playPause.gotoAndStop("pause");
textPos = 0;
}
// Pauses the music
function pauseIt():Void
{
pos = s.position;
s.stop();
}
// Pauses the music
function unPauseIt():Void
{
s.start(pos/1000);
}
// Music Controls
// Play/Pause 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();
}
// Mute Button
mute.onRollOver = function()
{
if(this._currentframe == 1) this.gotoAndStop("onOver");
else this.gotoAndStop("offOver");
}
mute.onRollOut = mute.onReleaseOutside = function()
{
if(this._currentframe == 10) this.gotoAndStop("on");
else this.gotoAndStop("off");
}
mute.onRelease = function()
{
if(this._currentframe == 10)
{
this.gotoAndStop("offOver");
s.setVolume(0);
}
else
{
this.gotoAndStop("onOver");
s.setVolume(75);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Text scroller bonus code
var size:Number = 21;
var textPos:Number = 0;
var intervalID:Number = setInterval(scroller, 1000);
function scroller():Void
{
var t:String = (sa[cps].artist + " - " + sa[cps].track);
if(textPos+size < t.length)
{
textPos++;
trackInfo.text = (sa[cps].artist + " - " + sa[cps].track).substring(textPos, textPos+size);
}
else
{
clearInterval(intervalID);
intervalID = setInterval(scroller2, 1000);
}
}
function scroller2():Void
{
var t:String = (sa[cps].artist + " - " + sa[cps].track);
if(textPos > 0)
{
textPos--;
trackInfo.text = (sa[cps].artist + " - " + sa[cps].track).substring(textPos, size);
}
else
{
clearInterval(intervalID);
intervalID = setInterval(scroller, 1000);
}
}
Re: [ActionScript 2.0] Music Randomization?
Around the area of:
Code:
if(cps == sa.length - 1)
{
cps = 0;
s.loadSound(sa[cps].earl, true);
}
else
{
s.loadSound(sa[++cps].earl, true);
}
You're going to need to check if it is the first time you have loaded the playlist or if the playlist is restarting (after reaching the end). Fairly simple just make a new variable to hold if the player has restarted increasing it after each playlist reset.
If you do not do this then each time the player would go to randomly select a song, by generating a random index for the song array (sa) it would randomly select a song after each playlist reset and not at the beginning song.
Therefore the previous mentioned variable that holds the playlist reset count should be referenced and if it is greater than one, the current playing song (cps) should be set to the first song (0).
However, I'm not familiar with actionscript so you're still left with figuring this out yourself since I'm not sure if what I said will work., but I think it should. hell I'm not even sure if this is what you're asking for xD
Re: [ActionScript 2.0] Music Randomization?
Well thanks, but i barely know any actionscript at all :(
i shall look for some more guides on it though.
Re: [ActionScript 2.0] Music Randomization?
Well wait before you do that because the XML file you're loading appears to be loaded and converted from an SQL Database, right?
You can do one of two things to make it random in your PHP File instead:
1.) You can make the order of the list random by adding this to your SQL query: "ORDER BY RND()"
2.) Or you can load all of the songs in order, and make an array of all of the songs. From there, you can use PHP to return this as your first song: "$songs_array[rand(0,(count($songs_array)-1))]"
Hope that helped
Re: [ActionScript 2.0] Music Randomization?
i did try to do it in php, but i didn't realize that the ORDER BY RND() function existed, thanks :]