gpt4 book ai didi

actionscript-3 - Flex播放一系列声音文件

转载 作者:行者123 更新时间:2023-12-03 00:49:41 26 4
gpt4 key购买 nike

因此,我正在制作一个基于输入播放一系列声音文件的应用程序。例如,输入可以是“Smile Cry Scream”,然后依次播放三个对应的声音。现在,我播放声音,然后启动计时器,完成后会触发下一个声音,依此类推。我不使用Timer的delay参数,因为它有一些小问题。是否有更好的方法来做到这一点,尤其是当声音之间的间隔变小时?

最佳答案

您可以听Event.SOUND_COMPLETE对象上的SoundChannel事件,以从声音数组中启动新声音(如果还有更多要启动的声音)。像这样:

var _sounds:Vector.<Sound>=new Vector.<Sound>();
var _sc:SoundChannel;
var _isPlaying:Boolean=false;
function channelASound(sound:Sound):void {
_sounds.push(sound);
checkPlay();
}
function checkPlay(e:Event=null):void {
if ((_isPlaying)&&(e==null)) return; // we've been called from channelASound
// with playback still going
if (_sc) _sc.removeEventListener(Event.SOUND_COMPLETE,checkPlay);
// otherwise we need to start another playback. Cleaning up first
if (_sounds.length==0) {
// nothing more to play. Not the case if called from channelASound
_sc=null;
_isPlaying=false;
return;
}
_sc=_sounds.shift().play();
_sc.addEventListener(Event.SOUND_COMPLETE,checkPlay);
_isPlaying=true;
}

它是如何工作的:您按所需的顺序为每个“微笑哭声”或其他声音序列(每个 call 一个)调用 channelASound。然后调用 checkPlay(),以检查是否有回放以及是否还有其他回放。如果我们只是引导一些内容,将会有更多的回放,但是如果当前正在进行回放,则 isPlaying将为true,我们将在此返回。否则,我们将使用 _sounds.pop()作为下一个要播放的声音开始新的播放,将 channel 分配给 _sc,添加一个侦听器并将 _isPlaying设置为true。如果函数是由侦听器触发的,则event参数不为null,因此我们不会尽快返回,而是从 _sc中清除旧数据和侦听器,并检查是否有更多回放。如果没有播放,我们将flag设置为false并清除 _sc变量,如果有更多播放,我们将开始下一个声音。

关于actionscript-3 - Flex播放一系列声音文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22836398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com