【问题标题】:AS3 play sounds in an array in a sequenceAS3 按顺序播放数组中的声音
【发布时间】:2012-03-20 18:51:17
【问题描述】:

这是在 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){
}

【问题讨论】:

    标签: actionscript-3 flash actionscript


    【解决方案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;
        }
    }
    

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

    【讨论】:

    • 谢谢,这帮助了我。根据您的建议,我能够通过分解它来解决这个问题。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2017-12-21
    相关资源
    最近更新 更多