[Embed('sounds/music1.mp3')]
public var Music1:Class;
[Embed('sounds/music2.mp3')]
public var Music2:Class;
[Embed('sounds/music3.mp3')]
public var Music3:Class;
public var music:Array;
public var currentSongIndex:int;
public function complete():void {
stage.scaleMode = StageScaleMode.SHOW_ALL;
stage.frameRate = 32;
music = new Array();
music.push(new Music1());
music.push(new Music2());
music.push(new Music3());
currentSongIndex = Math.floor( Math.random() * music.length );
var playFirst:SoundAsset = music[currentSongIndex] as SoundAsset;
playFirst.addEventListener(Event.COMPLETE, songFinished);
playFirst.play();
}
public function PlaySongFromIndex(songIndex:int){
var playFirst:SoundAsset = music[currentSongIndex] as SoundAsset;
playFirst.addEventListener(Event.COMPLETE, songFinished);
playFirst.play();
}
public function songFinished(e:Event){
if(currentSongIndex < music.Length){
currentSongIndex++;
PlaySongFromIndex(currentSongIndex);
} else {
currentSongIndex=0;
}
}
我正在尝试循环播放嵌入音乐,但只播放了第一首随机歌曲,然后只是沉默...无法理解为什么下一首歌不播放,谁能告诉我?
答案 0 :(得分:0)
在你的complete
处理程序的条件中,你正在测试music.Length
(注意大写的L),它会在执行时立即抛出错误。您还需要修复当前允许索引增加超出数组范围的测试(请记住,数组元素是0索引的)。
此外,由于您未从PlaySongFromIndex
条件调用else
方法,因此程序将不会每三次播放第一首歌曲一次。
尝试使用以下代码更新代码:
public function songFinished(e:Event){
if(currentSongIndex < music.length - 1){
currentSongIndex++;
} else {
currentSongIndex=0;
}
PlaySongFromIndex(currentSongIndex);
}