如何可靠地检查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;
}
}
}
关于flash - 检查SoundChannel是否正在播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/194150/