Jump to content


Photo

completely randomize playlist


  • Please log in to reply
16 replies to this topic

#1 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 03 April 2007 - 05:25 PM

Sorry, Pax, I just realised there was one other thing I needed!!!! :)

At the moment, using the mp3 player actionscript, the player starts on a random song, due to this line:

_global.song_nr = random(songfile.length);

HOWEVER, once it's started on the random song, it continues down the XML list in order; for example, if you have songs 1-10, and it starts on 7, it'll then play 8,9,10,1,2 etc. This also happens when you press the next or prev buttons.

Is there some way of getting the entire list to shuffle?

:winner:

#2 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 03 April 2007 - 05:31 PM

Ok, got it - very simple, really :)

You change this line (wherever you see it):

(song_nr == songfile.length-1) ? _global.song_nr=0 : _global.song_nr++;

...to this:

_global.song_nr = random(songfile.length);

Yay!!!

:winner:

#3 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 03 April 2007 - 09:23 PM

Might want to put a temporary variable that holds the current song_nr, then use an if statement to make sure that the newly generated random song number does not equal the song that is currently playing.

#4 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 03 April 2007 - 11:30 PM

I have no idea what you just said :lol: :blush:

#5 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 04 April 2007 - 07:58 AM

var tempHolder = _global.song_nr
_global.song_nr = random(songfile.length);

if(tempHolder == _global.song_nr ){
		_global.song_nr = random(songfile.length);
}

Somthing along those lines. Would probably best to put the song randomizer into a function so you can keep calling itself if the tempHolder == song_nr.

What this does is this:
Say you are currently listneing to song #5, and you hit the next button and it randomly picks 5 again...youre like...what the heck? Its the same song... So the code will just re-randomize the number if its the same as the one that is currently playing.

Hope that makes sense...Ive only been awake for 40 minutes....

#6 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 04 April 2007 - 09:01 AM

Great! Well, I added it, and it didn't break anything, so I'll take your word for it :blush: :lol:

#7 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 04 April 2007 - 09:17 AM

Lol, well, thats good. Only downside to the code I put there is that it will only check once. Say you get the same random number 3 times in a row, its not going to catch it. Which is why I suggested making that a function.

#8 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 04 April 2007 - 10:17 AM

Actually, yeah, I noticed that - is there a way of making it play through all the songs randomly, but playing every song before repeating one? I don't think there is :(

I also realised it doesn't make much sense to have the random code on the back button, but when I copied the normal code back in, it didn't stop the prev button from being random. Is there some way of getting it to go to the previous song?

We're still going!!!

:blush::):lol:

#9 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 04 April 2007 - 10:37 AM

Theres always a way to make Flash do what you want it to do...unless its something like cure cancer, or change your best friend into a pirate.

You'll have to sit down and decide how you would like to go about the logic for keeping track of what songs have been played, but one fairly simple (and not overly process intensive) way to do so is add each song that is played into an array.

Each time you randomize the song to play, you do two things - first, check to make sure the new random number is not in the array already, and then if it is not, you can assume its not the current song playing (therefore removing the need for the above code), and add that song to the array and play it. A simple "does this exist in the array" code is below:

// pass the song number into the function so it can check...aPlayedList is a new array that holds all of the songs that have been played.
function arrayChecker(nCheckForThis:Number){
	var bFound:Boolean = false;
	for(var ii:Number = 0; ii < aPlayedList.length; ii++){
		if(aPlayedList[ii] == nCheckForThis){
			bFound = true;
		}
	}
	if(!bFound){
		// song not found
		
		// add song to start of previous song list
		
		// add song to aPlayedList
		// play song
		// do the rest of the code
	}
}

You'll need to modify the code there with the right var names, and probably do a check to see if the aPlayedList.length == the total # of songs you have to clear the array and start it over.

To make the back button work, you'll need to have an array that holds all of the previously played songs, and just have it go to either the first or last item in that array (depending on how you set it up) and then remove that item from the array. This will let you keep going back to the start if you like, but if you hit the next button, you'll get a new random track.

Hope that helps.

#10 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 04 April 2007 - 10:46 AM

Wow, it's nearly 2am here, so that made me feel like going to sleep :blush:

I'll tackle the behemoth in the morning - cheers mate!!!

:lol:

#11 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 08 April 2007 - 05:28 AM

I must admit, this is way over my head, Pax :P

#12 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 08 April 2007 - 09:52 PM

Lol, well, if you want to send me your fla I can have a look at it, or post your code here for it and I'll see what I can do.

You should also write out the functionality in pseudo code (basically type the code out in normal words....)

I'll try and code around the pseudo code and that way it will hopefully make more sense when you look it over.

Edited by Pax, 08 April 2007 - 09:56 PM.


#13 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 09 April 2007 - 03:14 AM

Ok, well, here goes...

stop();
playlist = new XML();
playlist.ignoreWhite = true;
playlist.onLoad = function(success) {
	if (success) {
		_global.songname = [];
		_global.songband = [];
		_global.songfile = [];
		_global.songinfo = [];
		_global.songurl = [];
		for (var i = 0; i<playlist.firstChild.childNodes.length; i++) {
			_global.songname[i] = playlist.firstChild.childNodes[i].attributes.name;
			_global.songband[i] = playlist.firstChild.childNodes[i].attributes.band;
			_global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file;
			_global.songinfo[i] = playlist.firstChild.childNodes[i].attributes.info;
			// add the following line
			_global.songurl[i] = playlist.firstChild.childNodes[i].attributes.url;
		}
	}
	_root.createEmptyMovieClip("sound_mc", 1);
	_global.song_nr = random(songfile.length);
	// modify the following line
	_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songinfo[song_nr], songurl[song_nr]);
};
function timer(sound_obj) {
	time = sound_obj.position/1000;
	min = Math.floor(time/60);
	min = (min<10) ? "0"+min : min;
	sec = Math.floor(time%60);
	sec = (sec<10) ? "0"+sec : sec;
	timeDisplay_txt.text = min+":"+sec+"/"+totalDuration;
}
function duration (){
	timed = _root.sound_mc.sound_obj.duration/1000;
	mind = Math.floor(timed/60);
	mind = (mind<10) ? "0"+mind : mind;
	secd = Math.floor(timed%60);
	secd = (secd<10) ? "0"+secd : secd;
	totalDuration = mind+":"+secd;
}
// modify the following line
MovieClip.prototype.songStarter = function(file, name, band, info, url) {
	if (this.sound_obj) {
		this.sound_obj.stop();
		delete this.sound_obj;
	}
	this.sound_obj = new Sound(this);
	this.sound_obj.loadSound(file, true);
	this.onEnterFrame = function() {
		if (this.sound_obj.position>0) {
			delete this.onEnterFrame;
			display_txt.htmlText = name+" / "+band;
			info_mc.info_text.html = true; // add this line
			info_mc.info_text.htmlText = "<a href = \"" + url + "\" target =\"_blank\">" + info + "<a/>";  // modify this line
			timeInterval = setInterval(timer, 1000, this.sound_obj);
		} else {
			display_txt.text = "loading...";
			info_mc.info_text.text = "loading...";
		}
	};
	this.sound_obj.onSoundComplete = function() {
		clearInterval(timeInterval);
		timeDisplay_txt.text = "";
		_global.song_nr = random(songfile.length);
		_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songinfo[song_nr], songurl[song_nr]);
	};
this.sound_obj.setVolume(50);
};
btn_play.onRelease = function() {
	if (pause = true){ // no comment....
		this._parent.sound_mc.sound_obj.start(posiP) // start sound from the previously saved position
	}
	else {
	clearInterval(timeInterval);
	timeDisplay_txt.text = "";
	this._parent.sound_mc.songStarter(songfile[song_nr]);
	}
};
btn_pause.onRelease = function() { //pause button function
	this._parent.sound_mc.sound_obj.stop(); //stop the current sound
	posiP = _root.sound_mc.sound_obj.position / 1000; // save the current position in a new variable and divide by 1000 (ms -> sec)
	pause = true;//set the variable pause to true
}
btn_fw.onRelease = function() {
	clearInterval(timeInterval);
	timeDisplay_txt.text = "";
	var tempHolder = _global.song_nr
_global.song_nr = random(songfile.length);

if(tempHolder == _global.song_nr ){
		_global.song_nr = random(songfile.length);
}
	_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songinfo[song_nr], songurl[song_nr]);
};
btn_rev.onRelease = function() {
	clearInterval(timeInterval);
	_root.timeDisplay_txt.text = "";
	(song_nr == 0) ? _global.song_nr=songfile.length-1 : _global.song_nr--;
	_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songinfo[song_nr], songurl[song_nr]);
};
playlist.load("full-playlist.xml");
setInterval(duration,100);

So I guess, taking that code, I want to do this:

* New XML
* List attributes
* Create empty MC (sound_mc)
* Set randomize (although I must admit I don't understand how random(songfile.length) works - does it randomize based on the length of the song??
* Read each played song into an array
* start sound_mc (I think)
* do timer function (don't think we need to change that, as it's just telling the time display what to display, right?)
* do duration function (same thing, I guess?)
* Guessing here, but maybe modify the onSoundComplete function to check the array?
* Not sure we need to modify the play button
* Not sure we need to modify the pause button
* Modify the next button to check the array before choosing an unplayed song randomly
* Modify the prev button to check what the previous song in the array was, and play it
* Load playlist
* Set interval for duration code

* I'm also thinking there's extra modification needed for when ALL songs have been played into the array - probably an if statement?

There you go - probably embarassed myself badly with those uneducated guesses, but hopefully you'll forgive me ;)

...and as if I haven't given you enough to do, it would be lovely if you could take a look at my asfunction post, as it and this are the only things I need to address before launching the new site :D

Thank you very much for all your help, mate - literally couldn't have done it without you!!!!

:):):)

Edited by boogooloo1, 09 April 2007 - 03:15 AM.


#14 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 09 April 2007 - 09:43 AM

Alright, I made a few changes and wrote a couple functions to deal with everything we need here. Your buttons will just call functions which return values for your song_nr var. This reduces code and keeps things clean.

Your pseudo code is pretty good, but you can get more in depth with it and start writing things like:
if my album art variable equals undefined 
	show the default album art image
else load the album art image into the image holder movieclip

Its actually like writing code, just in normal, easier to understand language. Helps a bunch when planning out projects.

Anyways, the code for the mp3 player is below...you should be able to see where I added stuff. It might take a bit of tweaking (I dont have the fla so I couldnt test it...I just went over it a couple times...) The only problem I can see is with:
_global.song_nr = getRandomSong(songfile.length, true);

You might need to fix the scope to that function. (ie..._parent. getRandomSong(songfile.length, true):) or use _root.sound_mc.getRand.... not sure how the fla is built so....

Anyways, good luck mate.

stop();
playlist = new XML();
playlist.ignoreWhite = true;
playlist.onLoad = function(success) {
	if (success) {
		_global.songname = [];
		_global.songband = [];
		_global.songfile = [];
		_global.songinfo = [];
		_global.songurl = [];
		
		// Added for random song stuff
		_global.prevList = new Array();	// variable to hold the previously played songs so you can go back to them
		_global.playedSongs = new Array(); // holds all songs played
		//
		for (var i = 0; i<playlist.firstChild.childNodes.length; i++) {
			_global.songname[i] = playlist.firstChild.childNodes[i].attributes.name;
			_global.songband[i] = playlist.firstChild.childNodes[i].attributes.band;
			_global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file;
			_global.songinfo[i] = playlist.firstChild.childNodes[i].attributes.info;
			// add the following line
			_global.songurl[i] = playlist.firstChild.childNodes[i].attributes.url;
		}
	}
	_root.createEmptyMovieClip("sound_mc", 1);
	_global.song_nr = getRandomSong(songfile.length, true);//random(songfile.length);
	// modify the following line
	_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songinfo[song_nr], songurl[song_nr]);
};
function getRandomSong(nLen:Number, bAddPrev:Boolean):Number{
	if(bAddPrev){
		_global.prevList.push(_global.song_nr); // adds the currently playing song to the end of the previous list
	}
	
	var tmpNumber:Number = random(nLen);
	var bFound:Boolean = false;
	if(_global.playedSongs.length == nLen){
		_global.playedSongs = new Array(); // clears the array if all of the songs have been played
	}
	for(var ii:Number = 0; ii < _global.playedSongs.length; ii++){ // loops through the played songs array to make sure the new random number is not duplicated
		if(tmpNumber == _global.playedSongs[ii]){
			bFound = true;
			break;
		}
	}
	
	if(bFound){	// if the song is found
		getRandomSong(songfile.length, false); // search for a new random song
	} else {
		return tmpNumber;
	}
}
function getPrevSong():Number{
	if(_global.prevList.length >= 1){
		return _global.prevList.pop(); // the pop() method will remove the last value in an array and return it.
	} else {
		trace("No previously played songs");
	}
}
function timer(sound_obj) {
	time = sound_obj.position/1000;
	min = Math.floor(time/60);
	min = (min<10) ? "0"+min : min;
	sec = Math.floor(time%60);
	sec = (sec<10) ? "0"+sec : sec;
	timeDisplay_txt.text = min+":"+sec+"/"+totalDuration;
}
function duration (){
	timed = _root.sound_mc.sound_obj.duration/1000;
	mind = Math.floor(timed/60);
	mind = (mind<10) ? "0"+mind : mind;
	secd = Math.floor(timed%60);
	secd = (secd<10) ? "0"+secd : secd;
	totalDuration = mind+":"+secd;
}
// modify the following line
MovieClip.prototype.songStarter = function(file, name, band, info, url) {
	if (this.sound_obj) {
		this.sound_obj.stop();
		delete this.sound_obj;
	}
	this.sound_obj = new Sound(this);
	this.sound_obj.loadSound(file, true);
	this.onEnterFrame = function() {
		if (this.sound_obj.position>0) {
			delete this.onEnterFrame;
			display_txt.htmlText = name+" / "+band;
			info_mc.info_text.html = true; // add this line
			info_mc.info_text.htmlText = "<a href = \"" + url + "\" target =\"_blank\">" + info + "<a/>";  // modify this line
			timeInterval = setInterval(timer, 1000, this.sound_obj);
		} else {
			display_txt.text = "loading...";
			info_mc.info_text.text = "loading...";
		}
	};
	this.sound_obj.onSoundComplete = function() {
		clearInterval(timeInterval);
		timeDisplay_txt.text = "";
		_global.song_nr = getRandomSong(songfile.length, true);
		_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songinfo[song_nr], songurl[song_nr]);
	};
this.sound_obj.setVolume(50);
};
btn_play.onRelease = function() {
	if (pause = true){ // no comment....
		this._parent.sound_mc.sound_obj.start(posiP) // start sound from the previously saved position
	}
	else {
	clearInterval(timeInterval);
	timeDisplay_txt.text = "";
	this._parent.sound_mc.songStarter(songfile[song_nr]);
	}
};
btn_pause.onRelease = function() { //pause button function
	this._parent.sound_mc.sound_obj.stop(); //stop the current sound
	posiP = _root.sound_mc.sound_obj.position / 1000; // save the current position in a new variable and divide by 1000 (ms -> sec)
	pause = true;//set the variable pause to true
}
btn_fw.onRelease = function() {
	clearInterval(timeInterval);
	timeDisplay_txt.text = "";
	var tempHolder = _global.song_nr
	_global.song_nr = getRandomSong(songfile.length, true);
	_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songinfo[song_nr], songurl[song_nr]);
};
btn_rev.onRelease = function() {
	clearInterval(timeInterval);
	_root.timeDisplay_txt.text = "";
	//(song_nr == 0) ? _global.song_nr=songfile.length-1 : _global.song_nr--;
	_global.song_nr = getPrevSong();
	_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songinfo[song_nr], songurl[song_nr]);
};
playlist.load("full-playlist.xml");
setInterval(duration,100);


#15 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 09 April 2007 - 07:06 PM

G'day mate,

You're a legend!!! It's going to take a while to completely test, as it's not working 100% at the moment, but I'm sure I can get it happening with what you've given me - thank you!!!

:biggrin: :tiphat: :)

#16 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 10 April 2007 - 07:33 AM

:closedeyes: :doh:

Actually, I'm not sure I can figure it out, as I'd previously posted so confidently :( Any chance I could pm you the fla?

Also, one other (hopefully simple) issue - because of the way I've got my navigation set up, it would be fantastic if I could somehow check whether or not the player is playing. If it is, leave it alone, and if it isn't, start playing. How would one go about doing that...?

It's a coincidence that your P2L username means "A small flat tablet adorned with a sacred image that worshippers kiss when offered the kiss of peace", because I have one of those with your name on it which I use daily...

;-)

#17 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 10 April 2007 - 08:06 AM

It's a coincidence that your P2L username means "A small flat tablet adorned with a sacred image that worshippers kiss when offered the kiss of peace", because I have one of those with your name on it which I use daily...

;-)


Lol, didn't realize that was one of the meanings of my name. I use it because its Latin for 'Peace'.

Anyways, fire me your fla and I'll take a look at it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users