2012-03-20 60 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