我正在尝试使用 Flash 制作一个简单的 mp3 播放器。歌曲是使用包含歌曲列表的 XML 文件加载的。下面的代码被插入到一个新的关键帧中。
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);
这里的问题在于 nextSong() 函数。我无法保留 currentIndex 的值。虽然每次调用函数时我都会增加 currentIndex,但值保持不变。请建议...
最佳答案
由于您的变量是在关键帧中定义的,因此每次命中关键帧时都会重新启动它。我敢打赌,将定义移到关键帧范围之外会有所帮助。例如,到 Flash 的根目录(虽然在 OOP 方面不太好)
关于apache-flex - 在多次调用的函数中增加全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4063317/