我有一个用作录制平台的程序,对于我播放的每个声音,我都会发出一个新的声音。
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播放文件后,我如何摆脱它?
发布于 2013-04-19 23:42:53
您可以明确说明您使用的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时,这只是将其设置为垃圾收集的公平游戏,而不是强制垃圾收集。
另一个注意事项是,我发布的这段代码只是一个示例,您可能希望考虑拥有几个保持活动的声道实例,并根据需要进行重用。在这种情况下需要管理,但这样您就不会不断地创建/终止声音通道。
https://stackoverflow.com/questions/16107539
复制相似问题