繁体   English   中英

检查SoundChannel是否正在播放声音

[英]Check if SoundChannel is playing sound

如何正确检查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;
        }
    }
}

我知道这很老了,但我发现这个链接我觉得很有帮助。 它解释了如何从某一点监视和播放文件。

http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d21.html

检查声音是否仍在播放而不使用任何管理器的方法之一是在两个连续的enterFrame侦听器调用中检查soundChannel.position,如果不匹配,则声音仍在播放。

private var oldPosition:Number;
function onEnterFrame(e:Event):void {
    var stillPlaying:Boolean;
    var newPosition=soundChannel.position;
    if (newPosition-oldPosition>1) stillPlaying=true; else stillPlaying=false;
    oldPosition=newPosition;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM