Actionscript 3 闪存通道限制和内存管理

Actionscript 3 闪存通道限制和内存管理,actionscript-3,Actionscript 3,我有一个程序,我用它作为录音平台,我播放的每一个声音都会产生一个新的声音 public function playAudioFile():void { trace("audio file:", currentAudioFile); sound = new Sound(); sound.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

我有一个程序,我用它作为录音平台,我播放的每一个声音都会产生一个新的声音

        public function playAudioFile():void {
            trace("audio file:", currentAudioFile);
            sound = new Sound();
            sound.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
            sound.addEventListener(Event.COMPLETE, soundLoaded);
            sound.load(new URLRequest(soundLocation + currentAudioFile));
        }

        public function soundLoaded(event:Event):void {
            sound.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
            sound.removeEventListener(Event.COMPLETE, soundLoaded);
            var soundChannel:SoundChannel = new SoundChannel();
            soundChannel = sound.play();
            soundChannel.addEventListener(Event.SOUND_COMPLETE, handleSoundComplete);
            trace('soundChannel?', soundChannel);
        }

        public function handleSoundComplete(event:Event):void {
            var soundChannel:SoundChannel = event.target as SoundChannel;
            soundChannel.stop();
            soundChannel.removeEventListener(Event.SOUND_COMPLETE,handleSoundComplete);
            soundChannel = null;
        }

32次之后,当我调用sound.play()(在soundLoaded中)时,我停止获取SoundChannel对象。但是,我不需要32个SoundChannel对象,因为我只连续播放这些声音,而不是同时播放。在我“使用”SoundChannel播放文件后,如何摆脱它

您可以明确说明您使用的声音频道:

var soundChannel:SoundChannel = new SoundChannel();
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE,handleSoundComplete);
然后,当您播放完声音后,可以将频道设置为null,以便将其标记为垃圾收集:

function handleSoundComplete(e:Event):void
{
   var soundChannel:SoundChannel = e.target as SoundChannel;
   soundChannel.removeEventListener(Event.SOUND_COMPLETE,handleSoundComplete);
   soundChannel = null;
}
请记住,当声音加载完成时,需要删除上面代码中显示的事件侦听器

还请记住,当您将某些内容设置为null时,这只是将其设置为垃圾收集的公平游戏,而不是强制垃圾收集


另一个注意事项是,我发布的这段代码只是一个示例,您可能希望考虑使用几个保持活动状态的声音通道实例,并根据需要重用它们。在这种情况下,需要进行管理,但这样您就不会不断创建/终止声音通道

谢谢你的回答,起初我以为这解决了问题,但我还是不断地犯同样的错误。我在我的帖子中编辑了代码。在播放了32个声音后,你是否收到了错误?还是需要更多?很高兴知道,正如我的回答所提到的,垃圾收集可能是现在的问题。