如何可靠地检查SoundChannel是否还在播放声音?
例如,
[Embed(source="song.mp3")] var Song: Class; var s: Song = new Song(); var ch: SoundChannel = s.play(); // how to check if ch is playing?
解决方法
我做了一些研究,我找不到查询任何对象的方法来确定是否正在播放声音.你必须编写一个包装类并自己管理它.
package
{
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
public class SoundPlayer
{
[Embed(source="song.mp3")]
private var Song:Class;
private var s:Song;
private var ch:SoundChannel;
private var isSoundPlaying:Boolean;
public function SoundPlayer()
{
s = new Song();
play();
}
public function play():void
{
if(!isPlaying)
{
ch = s.play();
ch.addEventListener(
Event.SOUND_COMPLETE,handleSoundComplete);
isSoundPlaying = true;
}
}
public function stop():void
{
if(isPlaying)
{
ch.stop();
isSoundPlaying = false;
}
}
private function handleSoundComplete(ev:Event):void
{
isSoundPlaying = false;
}
}
}

