# Need help w/ listBox + music

**URL:** <https://forum.kirupa.com/t/need-help-w-listbox-music/34571>\
**Category:** Uncategorized\
**Created:** [November 4, 2003, 1:56am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571 "2003-11-04T01:56:27Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francky683](https://avatars.discourse-cdn.com/v4/letter/f/87869e/32.png) [@Francky683](https://forum.kirupa.com/u/Francky683)\
**Post date:** [November 4, 2003, 1:56am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/1 "2003-11-04T01:56:27Z")

</div>

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 😉

Thanks,

~Francky683~

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 4, 2003, 2:03am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/2 "2003-11-04T02:03:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 5, 2003, 2:08am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/3 "2003-11-05T02:08:48Z")

</div>

plz guys i really need help … ;(

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 5, 2003, 3:50am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/4 "2003-11-05T03:50:12Z")

</div>

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:

```auto
music_list=metallica,beatles,bee gees&

```

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 5, 2003, 3:54am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/5 "2003-11-05T03:54:03Z")

</div>

Oh hes goooood

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 5, 2003, 10:54pm UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/6 "2003-11-05T22:54:13Z")

</div>

yea he is … thx for your time claudio 😉 … 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~

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 6, 2003, 1:32am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/7 "2003-11-06T01:32:10Z")

</div>

You need to apply the code on the main timeline. 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 6, 2003, 11:48am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/8 "2003-11-06T11:48:24Z")

</div>

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 😉

🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 6, 2003, 12:02pm UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/9 "2003-11-06T12:02:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 6, 2003, 9:26pm UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/10 "2003-11-06T21:26:57Z")

</div>

maybe but you’re the only one who replied 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2003, 2:58am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/11 "2003-11-07T02:58:10Z")

</div>

Yea, you got a point now… 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2003, 7:41pm UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/12 "2003-11-07T19:41:22Z")

</div>

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~

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2003, 1:54am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/13 "2003-11-08T01:54:15Z")

</div>

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

```auto
myTextField.text = which;

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2003, 4:58am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/14 "2003-11-08T04:58:29Z")

</div>

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 ???

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2003, 5:22am UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/15 "2003-11-08T05:22:44Z")

</div>

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 = “”;  
};

```auto
Hope this helps.
```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2003, 10:40pm UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/16 "2003-11-08T22:40:16Z")

</div>

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))

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2003, 10:47pm UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/17 "2003-11-08T22:47:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2003, 11:15pm UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/18 "2003-11-08T23:15:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 8, 2003, 11:24pm UTC](https://forum.kirupa.com/t/need-help-w-listbox-music/34571/19 "2003-11-08T23:24:29Z")

</div>

Anytime Francky 😉
