在flex AIR代码中一个接一个地播放声音文件

时间:2011-01-22 09:03:23

标签: flex actionscript-3 air

我有一组声音片段一个接一个地播放,序列中间有几个时间间隔。 就我而言,这是一个问题 - 接下来是四个选项。

当我编写下面的代码时,所有的audop文件同时启动。如何在中间有时间延迟,以便第二个剪辑仅在第一个剪辑结束后播放,而第三个剪辑仅在第二个选项结束时开始播放。

我使用的是Flex AIR AS 3.请参阅下面的代码。提前谢谢。

    private function playCoundClips(): void
    {
            //set audio clips

            var questionClipSource : String = "assets/quiz_voiceovers/" + questionCode + "Q.mp3";

            var optionAClipSource : String = "assets/quiz_voiceovers/" + questionCode + "a.mp3";
            var optionBClipSource : String = "assets/quiz_voiceovers/" + questionCode + "b.mp3";
            var optionCClipSource : String = "assets/quiz_voiceovers/" + questionCode + "c.mp3";
            var optionDClipSource : String = "assets/quiz_voiceovers/" + questionCode + "d.mp3";

            playThisClip(questionClipSource);

            playThisClip(optionAClipSource);
            playThisClip(optionBClipSource);

            playThisClip(optionCClipSource);
            playThisClip(optionDClipSource);

    } 


    private function playThisClip(clipPath : String) : void
    {
        try
        {
            clipPlayingNow = true;
            var soundReq:URLRequest = new URLRequest(clipPath); 
            var sound:Sound = new Sound(); 
            var soundControl:SoundChannel = new SoundChannel(); 

            sound.load(soundReq); 
            soundControl = sound.play(0, 0);
        }
        catch(err: Error)
        {
            Alert.show(err.getStackTrace());
        }
    }

由于 萨米特

3 个答案:

答案 0 :(得分:0)

问题是你正在产生多个异步调用。在Sound上实现完整的回调函数,然后在回调函数中调用playThisClip函数。 (你可以在打电话前预定时间睡觉)

答案 1 :(得分:0)

这对我有帮助 http://livedocs.adobe.com/flex/3/html/help.html?content=Working_with_Sound_09.html

需要为以下代码编写代码:


sound.addEventListener(Event.ENTER_FRAME, onEnterFrame);
soundControl.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

private function onEnterFrame(event:Event):void
{
var estimatedLength:int =       
    Math.ceil(sound.length / (sound.bytesLoaded / sound.bytesTotal));

var playbackPercent:uint = 
    Math.round(100 * (soundControl.position / estimatedLength));

}

private function onPlaybackComplete(event:Event):void 
{
    Alert.show("Hello!");
}

答案 2 :(得分:0)

时间延迟,是非常糟糕的主意(在99%的情况下)。 查看SOUND_COMPLETE事件(请参阅doc) 声音停止播放时会触发此事件。 所以,现在很容易按顺序播放声音。 一个简单的例子(未经测试,但想法在这里):

//declare somewhere a list of sounds to play
var sounds:Array=["sound_a.mp3","sound_a.mp3"];//sounds paths

//this function will play all sounds in the sounds parameter
function playSounds(sounds:Array):void{
  if(!sounds || sounds.length==0){
     //no more sound to play
     //you could dispatch an event here
     return;
  }
  var sound:Sound=new Sound();
  sound.load(new URLRequest(sounds.pop()));
  var soundChannel:SoundChannel = sound.play();
  soundChannel.addEVentListener(Event.SOUND_COMPLETE,function():void{
     soundChannel.removeEventListener(Event.SOUND_COMPLETE,arguments.callee);   
     playSounds(sounds);
  });
}
相关问题