6

如何可靠地检查 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?
4

3 回答 3

11

我做了一些研究,但找不到查询任何对象以确定声音是否正在播放的方法。您似乎必须编写一个包装类并自己管理它。


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;
        }
    }
}
于 2008-10-11T21:11:26.590 回答
2

我知道这真的很旧,但我发现这个链接我认为很有帮助。它解释了如何从某个点监视和播放文件。

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

于 2012-10-05T11:05:08.840 回答
1

检查声音是否仍在播放且不使用任何管理器的方法之一是在两个连续的 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;
}
于 2012-10-06T17:22:09.360 回答