Check if SoundChannel is playing sound

前端 未结 3 1696
陌清茗
陌清茗 2020-12-19 08:52

How to check reliably if a SoundChannel is still playing a sound?

For example,

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

var s: Song = new Song         


        
相关标签:
3条回答
  • 2020-12-19 09:27

    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.

    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;
    }
    
    0 讨论(0)
  • 2020-12-19 09:32

    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

    0 讨论(0)
  • 2020-12-19 09:35

    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;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题