首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >闪存SoundChannel限制和内存管理

闪存SoundChannel限制和内存管理
EN

Stack Overflow用户
提问于 2013-04-19 22:57:29
回答 1查看 529关注 0票数 0

我有一个用作录制平台的程序,对于我播放的每个声音,我都会发出一个新的声音。

代码语言:javascript
复制
        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播放文件后,我如何摆脱它?

EN

回答 1

Stack Overflow用户

发布于 2013-04-19 23:42:53

您可以明确说明您使用的soundChannel:

代码语言:javascript
复制
var soundChannel:SoundChannel = new SoundChannel();
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE,handleSoundComplete);

然后,当您播放完声音后,您可以将通道设置为null,以便将其标记为垃圾收集:

代码语言:javascript
复制
function handleSoundComplete(e:Event):void
{
   var soundChannel:SoundChannel = e.target as SoundChannel;
   soundChannel.removeEventListener(Event.SOUND_COMPLETE,handleSoundComplete);
   soundChannel = null;
}

请记住,当声音加载完成时,您需要删除在上面的代码中显示的那些事件侦听器。

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16107539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档