# So close

**URL:** <https://forum.kirupa.com/t/so-close/315473>\
**Category:** Uncategorized\
**Created:** [October 27, 2010, 9:07am UTC](https://forum.kirupa.com/t/so-close/315473 "2010-10-27T09:07:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr\_T4](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@Mr\_T4](https://forum.kirupa.com/u/Mr_T4)\
**Post date:** [October 27, 2010, 9:07am UTC](https://forum.kirupa.com/t/so-close/315473/1 "2010-10-27T09:07:27Z")

</div>

Hi all,i made an mp3 player that generates a report about which songs were downloaded,with the click of a button it sends the vars to php which in turn writes to a local file.

There is a problem though,whenever i click next or prev more than once and hit the download button, all of the songs that were skipped will report as downloaded.i have come far with this so i cant give up on it.

Below is the code and will be glad if someone can point out what could be wrong.as my code wil show you, i am still a begginer in as3 but i do my best.the only way i could tap into xml and utilize the vars for tracks was through this current setup of the code.  
Thanks.

//----------------------------xml-stuff--------------------------------------  
var myXMLLoader:URLLoader = new URLLoader();  
myXMLLoader.load(new URLRequest(“[http://localhost/playlist.xml](http://localhost/playlist.xml)”));  
myXMLLoader.addEventListener(Event.COMPLETE, processXML);  
function processXML(e:Event):void  
{  
var myXML:XML=new XML(e.target.data);  
my\_songs=myXML.SONG;  
my\_total=my\_songs.length();

myXMLLoader.removeEventListener(Event.COMPLETE, processXML);  
myXMLLoader=null;  
}  
//--------------------play song--------------------------  
function playSong(mySong:Number):void  
{  
var myAlbum=my\_songs[mySong].@ALBUM;  
var myTrack=my\_songs[mySong].@TRACK;  
var myArtist=my\_songs[mySong].@ARTIST;  
var myURL=my\_songs[mySong].@URL;  
album\_txt.text=myAlbum;  
artist\_txt.text=myArtist;  
track\_txt.text=myTrack;  
//removeEventListener(Event.COMPLETE, playSong);  
//trace(myTrack);

if (my\_channel)  
{  
my\_channel.stop();  
my\_channel.removeEventListener(Event.SOUND\_COMPLETE, onNext);  
}  
my\_music = new Sound();  
my\_music.load(new URLRequest(myURL));  
my\_channel=my\_music.play();  
my\_channel.addEventListener(Event.SOUND\_COMPLETE, onNext);  
addEventListener(Event.ENTER\_FRAME, enterFrameHandler);  
removeEventListener(Event.SOUND\_COMPLETE, onNext);  
//----------------------------------------------------------------------  
//-----------------------------download btn + POST-----------------------------

dwnld\_btn.addEventListener(MouseEvent.CLICK, post);  
count++;  
counter.text = count.toString();  
function post(e:Event):void  
{  
//------------------------------------------------------------  
//-------------------------------vars-------------------------------  
var loader:URLLoader = new URLLoader();  
var request:URLRequest = new URLRequest();  
var variables:URLVariables = new URLVariables();  
var date:Date = new Date();  
//trace(date);  
loader = new URLLoader();  
loader.addEventListener(IOErrorEvent.IO\_ERROR, errorHandler);  
loader.addEventListener(SecurityErrorEvent.SECURITY\_ERROR, errorHandler);

request = new URLRequest();  
loadFromNetwork();  
//----------------------------------------------------------------  
//----------------------------php conn---------------------

function loadFromNetwork():void {  
request.url = “[http://localhost/t4.php](http://localhost/t4.php)”;

variables.date = new Date();  
variables.artist = myArtist=my\_songs[mySong].@ARTIST;  
variables.count = count.toString();  
variables.there = myURL=my\_songs[mySong].@URL;  
trace(variables.there);  
request.data = variables;  
request.method = URLRequestMethod.POST ;  
loader.load(request);  
loader.removeEventListener(Event.COMPLETE, loadFromNetwork);

//---------response from php--------------------  
loader.addEventListener(Event.COMPLETE,t);  
function t(evt:Event):void{

```
    trace(loader.data);
}

```

}//-----------------------------------------------------------------  
//-------------------------error handler----------------------------  
function errorHandler(event:ErrorEvent):void {  
trace("errorHandler: " + event);  
if(event is IOErrorEvent) {  
loadFromNetwork();  
}  
}  
}  
}  
//---------------------------buttons-------------------------------  
next\_btn.addEventListener(MouseEvent.CLICK, onNext);  
function onNext(e:Event):void  
{  
current\_song++;  
if (current\_song\>=my\_total)  
{  
current\_song=0;

}

//-----------------time display-----next\_btn----------------------  
clearInterval(time\_int);  
time\_int = setInterval(calculateTime, 100);  
function calculateTime() {  
var time:Number = my\_channel.position / 1000;  
var min = Math.floor(time / 60);  
min = (min \< 10) ? “0” + min : min;  
var sec = Math.floor(time % 60);  
sec = (sec \< 10) ? “0” + sec : sec;  
time\_txt.text = min + “:” + sec;  
if (my\_channel.position \>= my\_music.length) {  
clearInterval(time\_int);  
}  
}  
playSong(current\_song);  
next\_btn.removeEventListener(Event.COMPLETE, onNext);  
}  
prev\_btn.addEventListener(MouseEvent.CLICK, onPrev);  
function onPrev(e:MouseEvent):void  
{  
current\_song–;  
if (current\_song\<0)  
{  
current\_song=my\_total-1;  
}  
//-------------------time-------------------------------  
clearInterval(time\_int);  
time\_int = setInterval(calculateTime, 100);  
function calculateTime() {  
var time:Number = my\_channel.position / 1000;  
var min = Math.floor(time / 60);  
min = (min \< 10) ? “0” + min : min;  
var sec = Math.floor(time % 60);  
sec = (sec \< 10) ? “0” + sec : sec;  
time\_txt.text = min + “:” + sec;  
if (my\_channel.position \>= my\_music.length) {  
clearInterval(time\_int);  
}  
}  
playSong(current\_song);  
prev\_btn.removeEventListener(Event.COMPLETE, onPrev);  
}  
pause\_btn.addEventListener(MouseEvent.CLICK, onPause);  
function onPause(e:MouseEvent):void  
{  
if (my\_channel)  
{  
song\_position=my\_channel.position;  
my\_channel.stop();  
song\_paused=true;  
}  
}  
play\_btn.addEventListener(MouseEvent.CLICK, onPlay);  
function onPlay(e:MouseEvent):void  
{  
if (song\_paused)  
{  
my\_channel=my\_music.play(song\_position);  
song\_paused=false;  
}  
else if (!my\_channel)  
{

playSong(current\_song);  
play\_btn.removeEventListener(Event.COMPLETE, onPlay);  
//-------------------time-------------------------------  
clearInterval(time\_int);  
time\_int = setInterval(calculateTime, 100);  
function calculateTime() {  
var time:Number = my\_channel.position / 1000;  
var min = Math.floor(time / 60);  
min = (min \< 10) ? “0” + min : min;  
var sec = Math.floor(time % 60);  
sec = (sec \< 10) ? “0” + sec : sec;  
time\_txt.text = min + “:” + sec;  
if (my\_channel.position \>= my\_music.length) {  
clearInterval(time\_int);  
}  
}

}  
}
