views:

33

answers:

1

I am trying to make a simple mp3 player using flash. The songs are loaded using an XML file which contains the song list. The following code is inserted into a new keyframe.

import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.*;
import flash.events.MouseEvent;
import flash.net.URLRequest; 
import flash.xml.*;

/* Defining The Elements and Variables of the Music Player */
var MusicLoading:URLRequest;                        
var music:Sound = new Sound();
var sc:SoundChannel;
var currentSound:Sound = music;
var CurrentPos:Number;                              
var xml:XML;
var songlist:XMLList;                               
var currentIndex:uint;                              
var XMLLoader:URLLoader = new URLLoader();          
/* ------------------------------------------------------ */


/* --------------------Load songs from the list-------------------- */
function success(e:Event):void 
{
    xml = new XML(e.target.data);
    songlist = xml.song;    //For adding a new song in XML File.
    MusicLoading = new URLRequest(songlist[0].file);
    music.load(MusicLoading);
    currentIndex = 0;
}
//If XML File Load successfully, it will be voided this actions:
XMLLoader.addEventListener(Event.COMPLETE, success);

//The Name of the XML File.
XMLLoader.load(new URLRequest("playlist.xml"));

//Play The Song
function playSong(e:Event):void 
{
    if(sc != null)
        sc.stop();

    sc = currentSound.play(CurrentPos);
}

//Pause The Song
function pauseSong(e:Event):void 
{
    CurrentPos = sc.position;
    sc.stop();
}


//Next Song Functions
function nextSong(e:Event):void 
{
    sc.stop();

trace(currentIndex + " ");

    currentIndex = currentIndex + 1;

    trace(currentIndex);

    var nextSongFunc:URLRequest = new URLRequest(songlist[currentIndex].file);
    var nextTitle:Sound = new Sound();
    nextTitle.load(nextSongFunc);
    currentSound = nextTitle;
    sc = currentSound.play();

    sc.addEventListener(Event.SOUND_COMPLETE, nextSong);
}

PauseBtn.addEventListener(MouseEvent.CLICK, pauseSong);
PlayBtn.addEventListener(MouseEvent.CLICK, playSong);
NextBtn.addEventListener(MouseEvent.CLICK, nextSong);

The problem over here is in the nextSong() function. I am unable to preserve the value of currentIndex. Although I am incrementing currentIndex every time the function is called, but the value remains unchanged. Please suggest ...

A: 

Since your variable is defined in the keyframe, it would re-initiated everytime the keyframe is hit. I bet moving the definition outside the keyframe scope would help. For example, to root of the Flash (although not too good in terms of OOP)

xandy
Could u please elaborate on how to do that ???
Ayan Sen
I am new to flash ... am pretty clueless at the moment ...
Ayan Sen
Since I have no clue about how your flash project looks like (and no easy way to post), I am not quite sure if that's the problem. But you mentioned that your above code is attached to the keyframe, right? Flash is quite interesting, it can program in almost complete OOP way, but also script way. For script attached to keyframe, it is executed EVERYTIME the key frame is played. ie, your "var currentIndex:uint;" is executed and re-init everytime. A quick hack is using "_root" global var: http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary633.html
xandy
_root is the global variable that every frame/object within the flash application can access to it, and the value would persist in the whole life-cycle of your application. As I mentioned before, this is not the best way, and of course you can have tons of other way to achieve, but that's another big issue including re-design or refactoring your program.
xandy
Thanks ... I'll try to introduce classes ...
Ayan Sen