我正在试图制作一个简单的使用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,但值保持不变。请建议...
我不认为这应该归类为Flex,因为您似乎根本就没有处理Flex框架。 – JeffryHouser 2010-10-31 15:17:53