Flash - Loading xml into listbox help! Mp3 player...

Associate
Joined
18 Oct 2002
Posts
158
Location
Manchester, UK
hi guys,

Ive been making an mp3 player in flash which works no problem, i have a dynamic text field reading from the xml file which displays the current song playing. I achieved this from following a tutorial....now however I've been trying to add a playlist which is done in a listbox reading from the same xml file.

This works, however what I cannot get to work is if I click on a song in the listbox, it won't start to play that song...I think it is because I have created 2 different arrays for the mp3 player itself and the playlist, can anyone take a look through my code and see where I am going wrong?

Cheers!

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("songs.xml");

//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");
}

//Pauses the music
function pauseIt():Void
{
	pos = s.position;
	s.stop();
}

//Unpauses the music
function unPauseIt():Void
{
	s.start(pos/1000);
}

//Stops the music
function stopIt():Void
{
	s.stop();
}

//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();
		logo_anim_mov.gotoAndPlay(2);
	}
	else
	{
		this.gotoAndStop("pauseOver");
		this._parent.unPauseIt();
		logo_anim_mov.gotoAndPlay(2);
	}
}

//Next button
next.onRollOver = function()
{
	this.gotoAndStop("nextOver");
}

next.onRollOut = next.onReleaseOutside = function()
{
	this.gotoAndStop("next");
}

next.onRelease = function()
{
	this._parent.playSong();
	logo_anim_mov.gotoAndPlay(2);
}

//Stop button
stop.onRollOver = function()
{
	this.gotoAndStop("stopOver");
}

stop.onRollOut = stop.onReleaseOutside = function()
{
	this.gotoAndStop("stop");
}

stop.onRelease = function()
{
	this._parent.stopIt();
	playPause.gotoAndStop("play");
	logo_anim_mov.gotoAndPlay(2);
}

//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);
		logo_anim_mov.gotoAndPlay(2);
	}
	else
	{
		this.gotoAndStop("onOver");
		s.setVolume(75);
		logo_anim_mov.gotoAndPlay(2);
	}
}

//Playlist
// create an XMl object

var sngList:XML = new XML();

// ignore white spaces

sngList.ignoreWhite = true;
sngList.onLoad = function() {

// put the nodes of xml into array

var songArray:Array = this.firstChild.childNodes;

// iterate through song array
// and add songs to the listbox

for (var i = 0; i<songArray.length; i++) {
songList.addItem(songArray[i].attributes.display, songArray[i].attributes.url);
}

// when xml is loaded play first item

playSong.play(songList.getItemAt(0).data);

// highlight first song in listbox

songList.selectedIndex = 0;
};

// create a listener that responds 
// to changes in the listbox

var sngListener:Object = new Object();
sngListener.change = function() {

// play the item selected in listbox

playSong.play(songList.getItemAt(songList.selectedIndex).data);
};

// register the event listener to listbox

songList.addEventListener("change", sngListener);

// load xml file

sngList.load("songs.xml");
 
Back
Top Bottom