mp3 flash player issues

Hi everyone,

I’m new to AS3 so please be as patient as you can.:slight_smile:

For the past 3 weeks or so, I’ve been trying to make a mp3 player that loads the songs via an XML file, based on the date of the playlist. I have few issues that are giving me some headaches.

First, I get this error when I try to change the date of the playlist, but I have no clue why. I just can’t see where the problem lies:

TypeError: Error #2007: Parameter text must be non-null.
	at flash.text::TextField/set text()
	at ddp_listen_fla::MainTimeline/add_btns()
	at ddp_listen_fla::MainTimeline/processXML()
	at flash.events::EventDispatcher/dispatchEventFunction()
	at flash.events::EventDispatcher/dispatchEvent()
	at flash.net::URLLoader/onComplete()

second issue is that if I set a new position of the currently playing song, when the song finishes playing it does not play next song no matter what. But if I don’t alter the sound progress it does play the next song…

third issue, kind of related to the previous one, is that if I set a new position of the currently playing song and the song hasn’t finished downloading the seek bar behaves strangely, moving backwards and forwards.

this is the code that I’m using. hopefully will help you understand my issues:

stop();
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.events.Event;

var currentDate = new Date();
var playlistDate = new Date();
var monthDigit:String = new String();
var dayDigit:String = new String();
var playlistLoader:URLLoader = new URLLoader();
var file_title:Array = new Array();
var file_url:Array = new Array();
var file_name:Array = new Array();
var nr_songs:Number = new Number(0);
var sound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
var song:Number = new Number();
var songPos:Number = new Number();
var duration:Number = new Number();
var total:Number = new Number();
var loaded:Number = new Number();
var repeatState:Number = new Number(0);
// 0 = repetarea este oprita
// 1 = doar playlistul curent
// 2 = redare continua a playlisturilor

addEventListener(Event.ENTER_FRAME, calcProgress);
retry_btn.addEventListener(MouseEvent.CLICK, reloadPlaylist);
nextPlaylist_btn.addEventListener(MouseEvent.ROLL_OVER, btn_roll_over);
nextPlaylist_btn.addEventListener(MouseEvent.ROLL_OUT, btn_roll_out);
nextPlaylist_btn.addEventListener(MouseEvent.CLICK, nextPlaylist);
prevPlaylist_btn.addEventListener(MouseEvent.ROLL_OVER, btn_roll_over);
prevPlaylist_btn.addEventListener(MouseEvent.ROLL_OUT, btn_roll_out);
prevPlaylist_btn.addEventListener(MouseEvent.CLICK, prevPlaylist);
playPause_btn.addEventListener(MouseEvent.CLICK, playPause);
stop_btn.addEventListener(MouseEvent.CLICK, stopPlay);
progress.progressDownload.addEventListener(MouseEvent.CLICK, setNewProgress);

retry_btn.buttonMode = true;
progress.progressDownload.buttonMode = true;
progress.progressDownload.scaleX = 0;
progress.progressFill.scaleX = 0;

currentDate.date = (currentDate.getDate() - currentDate.getDay() - 1);
playlistDate.date = (playlistDate.getDate() - playlistDate.getDay() - 1);

currentDate = new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate());
playlistDate = new Date(playlistDate.getFullYear(),playlistDate.getMonth(),playlistDate.getDate());


function main():void
{
	day.text = playlistDate.getDate();
	month.text = playlistDate.getMonth() + 1;
	year.text = playlistDate.getFullYear();
	//generate playlist file name
	//var url:String = new String("http://---web--site--adress---/playlist.php?date=");
	twoDigit();
	//var playlisturl:String = url.concat(playlistDate.getFullYear(),monthDigit,playlistDate.getMonth() + 1,dayDigit,playlistDate.getDate());
	var playlisturl:String ="playlist.xml";
	playlistLoader = new URLLoader(new URLRequest(playlisturl));
	playlistLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	playlistLoader.addEventListener(Event.COMPLETE, processXML);
	trace("playlist loaded");
}
main();

//load rss;
function processXML(event:Event):void
{
	var xml:XML = new XML(event.target.data);
	var il:XMLList = xml.channel.item;
	var mediaNS:Namespace = xml.namespace("media");
	xml.addNamespace(mediaNS);
	for (var i:uint = 0; i < xml.channel.item.length(); i++)
	{
		var item_url:XMLList = new XMLList(xml.channel.item*.mediaNS::content.(@type=="audio/mpeg3")[0].@url);
		var item_title:XMLList = new XMLList(xml.channel.item*.description);
		file_title* = String(item_title);
		file_url* = String(item_url);
		file_name* = file_url*.slice(file_url*.indexOf("["),-4);
		nr_songs++;
	}
	playlistLoader.removeEventListener(Event.COMPLETE, processXML);
	playlistLoader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	playlistLoader = null;
	trace("got song info from xml file");
	add_btns();
}

//add btns
function add_btns()
{
	for (var i:int = 0; i <nr_songs; i++)
	{
		var new_btn:song_btn = new song_btn();
		new_btn.name = "song_btn_" + *;
		new_btn.x = 25 + i * 85;
		new_btn.y = 65;
		new_btn.song_title.text = file_title*;
		new_btn.addEventListener(MouseEvent.ROLL_OVER, btn_roll_over);
		new_btn.addEventListener(MouseEvent.ROLL_OUT, btn_roll_out);
		new_btn.addEventListener(MouseEvent.CLICK, getSong_nr);
		//new_btn.buttonMode = true;
		this.addChild(new_btn);
	}
	trace("buttons added");
}

function remove_btns()
{
	for (var i:int=0; i<nr_songs; i++)
	{
		if(getChildByName("song_btn_"+i)){
		removeChild(getChildByName("song_btn_"+i));
		}
	}
	trace("buttons removed");
}

function getSong_nr(event:MouseEvent):void
{
	song = Number(event.currentTarget.name.slice(-1));
	playSong(song);
}

//play song
function playSong(song:Number):void
{
	if (channel)
	{
		channel.stop();
	}
	sound = new Sound();
	sound.load(new URLRequest (file_url[song]));
	sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	sound.addEventListener(ProgressEvent.PROGRESS, loadingSong);
	channel = sound.play();
	channel.addEventListener(Event.SOUND_COMPLETE, nextSong);
	playPause_btn.gotoAndStop("pause");
	currentSong.text = file_name[song];
	trace("playing song: "+(song+1));

}

//stop;
function stopPlay(event:Event):void
{
	playPause_btn.gotoAndStop("play");
	channel.stop();
	songPos = 0;
	progress.progressDownload.scaleX = 0;
	progress.progressFill.scaleX = 0;
	currentSong.text == "";
}

function prevPlaylist(event:Event):void
{
	playlistDate = new Date(year.text,Number(month.text) - 1,day.text);
	playlistDate.date -=  7;
	trace("prev playlist called");
	remove_btns();
	main();
}
function nextPlaylist(event:Event):void
{
	playlistDate = new Date(year.text,Number(month.text) - 1,day.text);
	if (playlistDate < currentDate)
	{
		playlistDate.date +=  7;
		trace("next playlist called");
		remove_btns();
		main();
	}
}

//next song;
function nextSong(event:Event)
{
	song++;
	if (song == nr_songs)
	{
		song = 0;
	}
	trace("new song called");
	playSong(song);
}

function loadingSong(event:ProgressEvent):void
{

	total = sound.bytesTotal;
	loaded = sound.bytesLoaded;
	progress.progressDownload.width = loaded / total * 300;

	if (total == loaded)
	{
		sound.removeEventListener(ProgressEvent.PROGRESS, loadingSong);
	}
}

function setNewProgress(event:MouseEvent):void
{
	var soundDist:Number = progress.progressDownload.mouseX / progress.progressDownload.width;
	if (soundDist < 0)
	{
		soundDist = 0;
	}
	if (soundDist > 1)
	{
		soundDist = 1;
	}
	channel.stop();
	channel = sound.play(sound.length * soundDist);
	progress.progressFill.scaleX = soundDist;

	playPause_btn.gotoAndStop("pause");

}

function calcProgress(event:Event):void
{
	var percentLoaded:Number = sound.bytesLoaded / sound.bytesTotal*100;
	var approxProgress:Number  = channel.position / sound.length*percentLoaded;
	progress.progressFill.scaleX = approxProgress/100;
}

//play pause btn
function playPause(event:Event):void
{
	if (channel)
	{
		if (playPause_btn.currentFrame == 2)
		{
			songPos = channel.position;
			playPause_btn.gotoAndStop("play");
			channel.stop();
		}
		else
		{
			channel = sound.play(songPos);
			playPause_btn.gotoAndStop("pause");
		}
	}
}

// get month and date with 2 digit
function twoDigit():void
{
	if (Number(month.text) < 10)
	{
		monthDigit = "0";
	}
	else
	{
		monthDigit = "";

	}
	if (Number(day.text) < 10)
	{
		dayDigit = "0";
	}
	else
	{
		dayDigit = "";
	}
}

function reloadPlaylist(event:Event):void
{
	remove_btns();
	main();
}

//button animations
function btn_roll_over(event:MouseEvent):void
{
	event.currentTarget.roll_anim.play();
}

function btn_roll_out(event:MouseEvent):void
{
	event.currentTarget.roll_anim.play();
}

function ioErrorHandler(event:Event):void
{
}

I’m attaching the fla file as well as the XML file I’m using - just in case. Please help me with this and point in the right direction.
Thanks!