2011-01-22 62 views
0

我有一組聲音剪輯,它們以一系列時間間隔連續播放。 就我而言,它是一個問題 - 隨後是四個選項。在flex中陸陸續續播放聲音文件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()); 
     } 
    } 

感謝 薩米特

回答

0

問題是你正在產卵多個異步調用。在Sound上實現一個完整的回調函數,然後在回調函數內調用你的playThisClip函數。 (你可以睡了預定時間之前調用)

+0

代碼如何睡眠預定義的時間?我沒有得到一個直接的sleep()或wait()方法。我們可以花時間等,但我認爲解決方案不是那麼複雜。 – dhalsumit 2011-01-22 09:14:29

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!"); 
} 

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