flash – 检查SoundChannel是否正在播放声音

前端之家收集整理的这篇文章主要介绍了flash – 检查SoundChannel是否正在播放声音前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何可靠地检查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/857193.html

猜你在找的Flash相关文章