0

録音プラットフォームとして使用するプログラムがあり、再生するサウンドごとに新しいサウンドを作成します。

        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 を削除するにはどうすればよいですか?

4

1 に答える 1

2

使用する 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 に設定すると、ガベージ コレクションの公正なゲームとして設定されるだけで、ガベージ コレクションは強制されないことに注意してください。

もう 1 つの注意点として、私が投稿したこのコードは単なる例であり、複数のサウンド チャネル インスタンスをアクティブに保ち、必要に応じて再利用することを検討することをお勧めします。その場合は管理が必要ですが、サウンドチャンネルを常に作成/削除する必要はありません。

于 2013-04-19T15:42:52.900 に答える