0
votes

I have a set of sound clips to be played one after another in a sequence with couple of time interval inbetween. In my case, its a question - followed by the set of four options.

When I write the below code, all the audop files start together at same time. How can I had time delay inbetween, so that the second clip plays only after the first one is over, and the third one starts playing, only when the second option is over.

I am with Flex AIR AS 3. See code below. Thanks in advance.

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

Thanks Sumit

3

3 Answers

0
votes

The problem is you are spawning multiple asynchronous calls. Implement a complete call back function on Sound and then call your playThisClip function inside the callback function. (You can sleep for predefined time before calling)

0
votes

Time delay, is a very bad idea (in 99% of the case). Have a look at the SOUND_COMPLETE event (see the doc) This event is triggered when the sound stops playing. So, it's now very easy to play sounds in sequence. A simple example (not tested but idea is here) :

//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);
  });
}
0
votes

This helped me http://livedocs.adobe.com/flex/3/html/help.html?content=Working_with_Sound_09.html

One need to write code for:


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