AS3按顺序播放数组中的声音

时间:2012-03-20 18:51:17

标签: actionscript-3 flash actionscript

这是在Flash CS5.5中编程的:

我想按一个按钮,一次播放整个阵列一个声音。当第一个声音停止时,第二个声音开始,等等,直到最后一个声音播放。当最后一个声音结束时,所有声音应该停止,如果再次按下播放按钮,它应该从头开始,然后再次播放所有声音。

目前,要进入下一个声音,您必须再次按下该按钮。我在想SOUND_COMPLETE需要使用......我只是不确定如何,因此空函数。我只想按一次播放按顺序听完整个阵列。有什么想法吗?

var count;
var songList:Array = new Array("test1.mp3","test2.mp3","test3.mp3");

count = songList.length;
myTI.text = count;
var currentSongId:Number = 0;

playBtn.addEventListener(MouseEvent.CLICK, playSound);

function playSound(e:MouseEvent):void{
if(currentSongId < songList.length)
{
var mySoundURL:URLRequest = new URLRequest(songList[currentSongId]);        
var mySound:Sound = new Sound();
mySound.load(mySoundURL);
var mySoundChannel:SoundChannel = new SoundChannel();

mySoundChannel = mySound.play();
currentSongId++;
mySoundChannel.addEventListener(Event.SOUND_COMPLETE,handleSoundComplete)
}
if(currentSongId == songList.length)
{
    currentSongId = 0;
}
}

function handleSoundComplete(event:Event){
}

1 个答案:

答案 0 :(得分:1)

您应该使用函数来调整您的操作,这将使您的代码更具可读性。

private Array songList = new Array("test1.mp3", "test2.mp3");




public function onPlayBtnPressed(){
    currentSongIndex = 0;
    PlaySongFromIndex(currentSongIndex);
}


public function PlaySongFromIndex(songIndex:int){
    //do what ever here to simply play a song.
    var song:Sound = new Sound(songList[songIndex]).Play()
    //Addevent listener so you know when the song is complete
    song.addEventListener(Event.Complete, songFinished);
    currentSongIndex++;
}

public function songFinished(e:Event){
    //check if all the songs where played, if so resets the song index back to the start.
    if(currentSongIndex < listSong.Length){
        PlaySongFromIndex(currentSongIndex);
    } else {
        currentSongIndex=0;
    }
}

这不会编译它只是为了显示一个例子,希望这有帮助。