在flex中陆陆续续播放声音文件AIR代码
问题描述:
我有一组声音剪辑,它们以一系列时间间隔连续播放。 就我而言,它是一个问题 - 随后是四个选项。在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());
}
}
感谢 萨米特
答
问题是你正在产卵多个异步调用。在Sound上实现一个完整的回调函数,然后在回调函数内调用你的playThisClip函数。 (你可以睡了预定时间之前调用)
答
帮我 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!");
}
答
延时,是非常不好的想法(在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);
});
}
代码如何睡眠预定义的时间?我没有得到一个直接的sleep()或wait()方法。我们可以花时间等,但我认为解决方案不是那么复杂。 – dhalsumit 2011-01-22 09:14:29