Autoplay and track changes for mp3 player in flash and AS3

Hi

I am learning about AS3 coding and doing a simple mp3 player. Currently I have a list component that is updated from an xml file as in the image attached

How do I

a) Get the previous and next buttons to play the next track in the list

and

b) get the play button to play the first track by default if nothing else is selected. Currently when nothing is selected and you click play it gives an error.

I had some ideas on how to do this but I can t find any good clear explanations on how to access the data inside a list component.

The player

import flash.ui.Mouse;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.events.Event;
import fl.controls.List;
import flash.net.URLLoader;

var XMLLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var channel:SoundChannel = new SoundChannel();
var songURL:URLRequest = new URLRequest();
var playerObject:Object = new Object();
var pausePoint:Number = 0;
var mPlaying:Boolean;
var flagsc:Number=0;



//load the XML file containing the music playlist
XMLLoader.load(new URLRequest("playlist.xml"));


//Event listeners
XMLLoader.addEventListener(Event.COMPLETE, loadXML);
listTracklist.addEventListener(Event.CHANGE, trackListChange);
btnPlay.addEventListener(MouseEvent.CLICK,playMusic);
btnStop.addEventListener(MouseEvent.CLICK,stopMusic);
btnPause.addEventListener(MouseEvent.CLICK,pauseMusic);
//btnBack.addEventListener(MouseEvent.CLICK,backMusic);
//btnNext.addEventListener(MouseEvent.CLICK,fwdMusic);


//Functions

function trackListChange(event:Event) {

	if (flagsc==1) {
		channel.stop();
	} else if (flagsc==0) {
		flagsc=1;
	}

	playSound(event.target.selectedItem.data);
	
}

function playSound(url) {
	songURL.url = url;

	if (playerObject.sound!=null) {
		delete playerObject.sound;

	}

	playerObject.sound = new Sound();

	soundevents();

	playerObject.sound.load(songURL);
}

function soundevents():void {
	playerObject.sound.addEventListener(Event.COMPLETE, musicloaded);

}

function musicloaded(event:Event) {
	
	channel=playerObject.sound.play();
	mPlaying = true;
}

function playMusic(evt:MouseEvent):void
{
	if (!mPlaying) {
		channel = playerObject.sound.play(pausePoint);
		mPlaying = true;
	}
}

function pauseMusic(evt:MouseEvent) {
	if (mPlaying) {
		pausePoint = channel.position;
		channel.stop();
		mPlaying = false;

	}
}
//stop button
function stopMusic(evt:MouseEvent):void {
	if (mPlaying) {
		channel.stop();
		mPlaying = false;

	}
	pausePoint = 0;
}


function loadXML(e:Event):void
{
	xmlData = new XML(e.target.data);
	parseMusic(xmlData);
}

function parseMusic(musicInput:XML):void
{
	var musicName:XMLList = musicInput.tracks.mname;
	var musicURL:XMLList = musicInput.tracks.murl;
	
	for (var i:int = 0; i < musicName.length(); i++)
	{
		var nameElement:XML = musicName[i];
		var urlElement:XML = musicURL[i];
		listTracklist.addItem({label:musicName[i], data:musicURL[i]});
		

	}
}

Open in new window

LVL 1
QuintusSmitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QuintusSmitAuthor Commented:
Never mind, solved it myself

I will clean up the code and paste it here soon
0
QuintusSmitAuthor Commented:
Here is the final one that worked:

import flash.ui.Mouse;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.events.Event;
import fl.controls.List;
import flash.net.URLLoader;
import flashx.textLayout.elements.ListElement;

var XMLLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var channel:SoundChannel = new SoundChannel();
var songURL:URLRequest = new URLRequest();
var playerObject:Object = new Object();
var pausePoint:Number = 0;
var mPlaying:Boolean;
var flagsc:Number=0;
var currentIndex:Number = 0;


//load the XML file containing the music playlist
XMLLoader.load(new URLRequest("playlist.xml"));


//Event listeners
XMLLoader.addEventListener(Event.COMPLETE, loadXML);
listTracklist.addEventListener(Event.CHANGE, trackListChange);
btnPlay.addEventListener(MouseEvent.CLICK,playMusic);
btnStop.addEventListener(MouseEvent.CLICK,stopMusic);
btnPause.addEventListener(MouseEvent.CLICK,pauseMusic);
btnPrevious.addEventListener(MouseEvent.CLICK,backMusic);
btnNext.addEventListener(MouseEvent.CLICK,fwdMusic);


//Functions
function fwdMusic(evt:MouseEvent):void
{
	channel.stop();
	 var nextIndex:Number = currentIndex +1;
	 trace(nextIndex);
	 playSound(listTracklist.dataProvider.getItemAt(nextIndex).data)
	 listTracklist.selectedIndex = nextIndex;
	 currentIndex = nextIndex;
}

function backMusic(evt:MouseEvent):void
{
	channel.stop();
	 var nextIndex:Number = currentIndex - 1;
	 trace(nextIndex);
	 playSound(listTracklist.dataProvider.getItemAt(nextIndex).data)
	 listTracklist.selectedIndex = nextIndex;
	 currentIndex = nextIndex;
}

function trackListChange(event:Event) {

	if (flagsc==1) {
		channel.stop();
	} else if (flagsc==0) {
		flagsc=1;
	}

	playSound(event.target.selectedItem.data);
	currentIndex = event.target.selectedIndex;

	
	
}

function playSound(url) 
{
	songURL.url = url;

	if (playerObject.sound!=null) {
		delete playerObject.sound;

	}

	playerObject.sound = new Sound();

	soundevents();

	playerObject.sound.load(songURL);
}


function soundevents():void {
	playerObject.sound.addEventListener(Event.COMPLETE, musicloaded);

}

function musicloaded(event:Event) {
	
	channel=playerObject.sound.play();
	mPlaying = true;

}

function playMusic(evt:MouseEvent):void
{

		if (!mPlaying) {
				channel = playerObject.sound.play(pausePoint);
				mPlaying = true;
		}
	
	
}

function pauseMusic(evt:MouseEvent) {
	if (mPlaying) {
		pausePoint = channel.position;
		channel.stop();
		mPlaying = false;

	}
}
//stop button
function stopMusic(evt:MouseEvent):void {
	if (mPlaying) {
		channel.stop();
		mPlaying = false;

	}
	pausePoint = 0;
}


function loadXML(e:Event):void
{
	xmlData = new XML(e.target.data);
	parseMusic(xmlData);
}

function parseMusic(musicInput:XML):void
{
	var musicName:XMLList = musicInput.tracks.mname;
	var musicURL:XMLList = musicInput.tracks.murl;
	
	for (var i:int = 0; i < musicName.length(); i++)
	{
		var nameElement:XML = musicName[i];
		var urlElement:XML = musicURL[i];
		listTracklist.addItem({label:musicName[i], data:musicURL[i]});
		

	}
	listTracklist.selectedIndex = 0;
	playSound(listTracklist.dataProvider.getItemAt(0).data);

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.