Skip to main content
December 3, 2013
Question

How to stop / destroy a sound object?

  • December 3, 2013
  • 2 replies
  • 524 views

I have a function that instanciates a MovieClip class:

public function loadPart(spec:int) {

            if(part != null) {

                part.destruct();

                removeChild(part);

            }

            pClass = getDefinitionByName('part'+spec) as Class;

            part = new pClass();

            addChildAt(part, 0);         

        }

The class code in general looks like this:

package  {

    import flash.display.MovieClip;

    import flash.events.Event;

    import flash.events.MouseEvent;

    import flash.utils.getTimer;

    import flash.media.SoundChannel;

    import flash.media.Sound;

    import flash.media.SoundMixer;

    public class part0 extends MovieClip {

        ......

        public var snd:Sound;

        public var sch:SoundChannel = null;

        public function part0() {

            addEventListener(Event.ADDED_TO_STAGE, init);

        }

        private function init(e:Event) {

            removeEventListener(Event.ADDED_TO_STAGE, init);

            .......

            // sound

            snd = new part_0();

            sch = snd.play(1000);


        }

      

        private function soundSwitch(_mod:int = -1) {

            if(_mod) {

                sch = snd.play(pos);

            } else {

                pos = sch.position;

                sch.stop();

            }

        }

        public function destruct() {

            if(hasEventListener(Event.ENTER_FRAME)) {

                removeEventListener(Event.ENTER_FRAME, onEF);

            }

            // sound

            SoundMixer.stopAll();

        }

    }

}

If I spam the funcfion that instanciates this class, sooner or later I have multiple copies of the same sound playing.

Can anyone please tell me why it might be happening and how to fix it?

Thank you!

This topic is closed to new replies. Start a new post to keep the conversation going.

2 replies

Inspiring
December 3, 2013

SoundMixer.stopAll will stop all the sound, but you have to put it in the proper place, then only it will work. Just put that   SoundMixer.stopAll(); before the if condition.

And i am not seeing anywhere you are calling that destruct function

December 3, 2013

That doesn't change anything and why would it?

SoundMixer does stop all the sounds, the problem is that if I pause and unpause the sound inside the class and re-instanciate the class frequently (with the sound inside being paused or playing at the moment of re-instanciation), then at some point I have mupltiple copies of the sound playing.