Need help w/ listBox + music

Hi everybody,
I just joined this forum like 1 week ago and i posted about making a listBox that would be used to play music … I need help with a little code in the listBox :

music = new Sound(this);
list.addItem(“Music”, music.attachSound(“barry”));
list.addItem(“Music1”, music.attachSound(“beegees”));

“music” is the name used for the play/stop button. I want that when i’m choosing like the “Music1” it plays the song named beegees … With this code, when i’m clicking play it’s playing the second one and even if i click on “Music” it’s still playing the second one … Could someone correct my code or tell me where i’m wrong … It would be really appreciated :wink:

Thanks,

~Francky683~

Btw if you can’t understand me plz tell me :wink: (sry i’m french and i can’t explain as well as you guys :wink: )

Firstly, great taste in music.
Secondly, do a search

plz guys i really need help … ;(

Okay, let me give it a shot.

1) First, create a listbox and give it an instance name of myListBox.

2) Now create a textfield to display the loading progress. Set it to dynamic and give it an instance name of myTextField.

3) Place 2 buttons onstage. Set their instance names to play_btn and stop_btn

4) Time to create your textfile that will hold the song list. Lets say you have 3 mp3 files called:[list]**
[]metallica.mp3
[
]beatles.mp3
[*]bee gees.mp3
**[/list]
Now open notepad and write the following:

music_list=metallica,beatles,bee gees&

5) Finally, the actionscript. Place this code on a frame:

my_sound = new Sound();
loadText = new LoadVars();
loadText.load("music_list.txt");
loadText.onLoad = function(success) {
	if (success) {
		var my_array = loadText.music_list.split(',').sort();
		for (var i = 0; i<my_array.length; i++) {
			myListBox.addItem(my_array*);
		}
	} else {
		myListBox.addItem("Error loading contents!");
	}
};
loadSong = function () {
	var which = myListBox.getSelectedItem().label;
	my_sound.loadSound(which+".mp3", false);
	preload();
};
preload = function () {
	this.createEmptyMovieClip("temp", 1000);
	temp.onEnterFrame = function() {
		var t = my_sound.getBytesTotal(), l = my_sound.getBytesLoaded();
		if (t && !isNaN(t)) {
			var percent = Math.floor(l*100/t);
			if (percent == 100) {
				myTextField.text = "Playing";
				my_sound.start(0, 1);
				this.removeMovieClip();
			} else {
				myTextField.text = percent+" %";
			}
		}
	};
};
play_btn.onPress = loadSong;
stop_btn.onPress = function() {
	my_sound.stop();
	myTextField.text = "";
};

This should work. Tell me if you have trouble.

Oh hes goooood

yea he is … thx for your time claudio :wink: … btw yea i got probs in the Script syntax … here thay are :

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 5: Statement must appear within on/onClipEvent handler
my_sound = new Sound();

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 6: Statement must appear within on/onClipEvent handler
loadText = new LoadVars();

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 7: Statement must appear within on/onClipEvent handler
loadText.load(“music_list.txt”);

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 8: Statement must appear within on/onClipEvent handler
loadText.onLoad = function(success) {

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 18: Statement must appear within on/onClipEvent handler
loadSong = function () {

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 23: Statement must appear within on/onClipEvent handler
preload = function () {

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 39: Statement must appear within on/onClipEvent handler
play_btn.onPress = loadSong;

Scene=Scene 1, Layer=Layer 1, Frame=1: Line 40: Statement must appear within on/onClipEvent handler
stop_btn.onPress = function() {

… dunno what to do so i’m asking … could you correct it if it’s not too asked too much ???

Thanks,

~Francky683~

You need to apply the code on the main timeline. :wink:

WAHOOOOOOOOOOOOOOOOOOOOOOOOO !!!

You ruuuuuuuuuuuuulzzzzzzzzzzzz !!!

I Love You :p:

Thanks again !!!

~Francky683~

Btw you soulh post a tutorial about that a lot of peeps are searching for that kind of thing on the net :wink:

:slight_smile:

Glad i could help =)
I dont know about the tutorial, im sure someone’s got a better code :wink:

maybe but you’re the only one who replied :wink:

Yea, you got a point now… :stuck_out_tongue:

i’ve got another question … how can i customize the text displayed in myTextField ??? like displaying the songs title/artist and not the little “Playing” …

Thanks,

~Francky683~

Remove the var from [AS]var which = myListBox.getSelectedItem().label;[/AS]And use this:

myTextField.text = which;

yea thx but that’s only a part of what i meant … could i customize what is written ??? not only the song title displayed in the listBox but anything else ???

Yes you can.
Heres one way:
Store 2 variables in the txt file; one for the song list and another for the captions
So your textfile should look like this:[AS]music_list=bee gees,metallica, beatles&captions=Bee Gees - I Started A Joke,Metallica - One, Beatles - Come Together&[/AS]And your code:[AS]my_sound = new Sound();
loadText = new LoadVars();
loadText.load(“music_list.txt”);
loadText.onLoad = function(success) {
if (success) {
var my_array = loadText.music_list.split(’,’);
caption_array = loadText.captions.split(’,’);
for (var i = 0; i<my_array.length; i++) {
myListBox.addItem(my_array*);
}
} else {
myListBox.addItem(“Error loading contents!”);
}
};
loadSong = function () {
var which = myListBox.getSelectedItem().label;
my_sound.loadSound(which+".mp3", false);
preload();
};
preload = function () {
this.createEmptyMovieClip(“temp”, 1000);
temp.onEnterFrame = function() {
var t = my_sound.getBytesTotal(), l = my_sound.getBytesLoaded();
if (t && !isNaN(t)) {
var percent = Math.floor(l*100/t);
if (l == t) {
myTextField.text = caption_array[myListBox.getSelectedIndex()];
my_sound.start(0, 1);
this.removeMovieClip();
} else {
myTextField.text = percent+" %";
}
}
};
};
play_btn.onPress = loadSong;
stop_btn.onPress = function() {
my_sound.stop();
myTextField.text = “”;
};

Hope this helps.

oooooooooooooh yeah!
You’re tha king!

Thanks thousands of times!!!

~Francky683~

(Just a little question … is there a limit of variables to use in the text files ??? (cuz i’ll maybe add lyrics to the songs so i’ll just try, you did enough for mow :P))

Glad I could help;)
As for the limit of variables in text files, i really have no clue :cyclops:
Go ahead and try to add the lyricvs. If you get stuck, post here. Im sure someone will help you.

I succeded :smiley: … not that hard, i’m using the same code you used to display the song title … anyways thx again! :slight_smile:

Anytime Francky :wink: