2012-03-20 56 views
0

這是在Flash CS5.5編程:AS3中發揮陣列聲音序列中

我想按下一個按鈕,並通過在一時間整個陣列一個聲音播放。當第一個聲音停止時,第二個聲音開始,直到最後一個聲音播放。當最後一個聲音結束時,所有聲音都應該停止,如果再次按下播放按鈕,它應該從頭開始重新播放所有聲音。

目前,要進入下一個聲音,您必須再次按下按鈕。我在想,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

您應該使用函數來調整您的操作,這會使您的代碼更具可讀性。

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; 
    } 
} 

這不會編譯它只是爲了顯示一個例子,希望這有助於。

+0

謝謝,這幫了我。根據你的建議,我可以把它分解出來。乾杯! – user1129107 2012-03-29 06:30:52

+0

很高興我有任何幫助:) – 2012-03-29 12:14:22