J'essaye de faire un simple lecteur mp3 en utilisant le flash. Les chansons sont chargées en utilisant un fichier XML qui contient la liste des chansons. Le code suivant est inséré dans une nouvelle image clé.Incrémentation d'une variable globale dans une fonction qui est appelée plusieurs fois
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);
Le problème ici est dans la fonction nextSong(). Je suis incapable de préserver la valeur de currentIndex. Bien que j'incrémente currentIndex à chaque fois que la fonction est appelée, mais la valeur reste inchangée. Veuillez suggérer ...
Je ne pense pas que cela devrait être catégorisé comme Flex, car vous ne semblez pas du tout avoir affaire au framework Flex. – JeffryHouser