简体   繁体   English

检查SoundChannel是否正在播放声音

[英]Check if SoundChannel is playing sound

How to check reliably if a SoundChannel is still playing a sound? 如何正确检查SoundChannel是否还在播放声音?

For example, 例如,

[Embed(source="song.mp3")]
var Song: Class;

var s: Song = new Song();
var ch: SoundChannel = s.play();

// how to check if ch is playing?

I've done a little research and I can't find a way to query any object to determine if a sound is playing. 我做了一些研究,我找不到查询任何对象的方法来确定是否正在播放声音。 You'll have to write a wrapper class and manage it yourself it seems. 你必须编写一个包装类并自己管理它。


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;
        }
    }
}

I know this is really old but i found this link that i think is quite helpful. 我知道这很老了,但我发现这个链接我觉得很有帮助。 it explains how to monitor and play a file from a certain point. 它解释了如何从某一点监视和播放文件。

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

One of the ways to check if sound is still playing, and not using any managers, would be checking soundChannel.position in two consecutive enterFrame listener calls, if mismatched, then the sound is still playing. 检查声音是否仍在播放而不使用任何管理器的方法之一是在两个连续的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