Skip to main content
Participating Frequently
September 20, 2014
Answered

Stop sound when another sound plays

  • September 20, 2014
  • 2 replies
  • 8267 views

Hey guys,

Using Flash Pro CC 2014, Actionscript 3

I am working on a soundboard and use buttons that play sounds, and I want to make it that when a new sound plays, it automatically stops the current sound from playing, that way 2 sounds dont play at same time.

But I also have a button that stops all sounds, I am using the "Click to Stop All Sounds" code under Audio and Video in the Code Snippets.

So I am basically wanting to make sure I can do both, keep the stop all sounds button, but also have it when you press any sound button, it stops all sounds and starts playing the current button/sound. Any suggestions?

BTW I am setting up symbols as buttons, not movie clips incase you need to know for coding, but I can switch to movie clips if its easier. I was just used to buttons on older versions of Flash for simple stuff like this.

Thanks.

This topic has been closed for replies.
Correct answer kglad

Below is the exact code and it does not work the way it is supposed to. It is supposed to stop the current sound playing, and then immediately start playing the sound file that is on that button. However all this code does is stop any sounds at all from being played.

I'm not looking for a stop sounds only button, that is what the code below appears to be.

sound.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);

function fl_ClickToStopAllSounds(event:MouseEvent):void

{

    SoundMixer.stopAll();

}


the code you showed will stop all sounds (if there are any) currently playing, and nothing else.

i'm not sure why you would think it would '...immediately start playing the sound file that is on that button'.

if you wanted sound to stop all current sounds and play NewSound, use:

sound.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);

var sc:SoundChannel;

function fl_ClickToStopAllSounds(event:MouseEvent):void

{

    SoundMixer.stopAll();

var newSound:NewSound=NewSound();

sc=newSound.play();

}

2 replies

bethanyh6784532
New Participant
May 29, 2018

This new part

var newSound:NewSound=NewSound();

sc=newSound.play();

Do I need to replace "NewSound" with the button name or something?? or with the mp3 name?

Mine still doesn't work!

kglad
Adobe Expert
September 20, 2014

the best solution is to keep track of all your sound instances that are currently playing and stop them (actually, their soundchannel) explicitly.

the sloppy way that usually works well is to use

SoundMixer.stopAll();

Participating Frequently
September 20, 2014

Thanks for the reply, I am new to actionscript 3 but am a fast learner. Maybe I am doing this wrong, but when I add the code it simply stops the sounds, but it wont play the sound from the button that I just clicked on. I want it to play the sound button clicked on, and also stop any other sounds that are currently playing.

For Flash 8, as2, I would add this to the button:

on (press)

{

    stopAllSounds ();

}

On as3, does it need to say this?

button_2.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds);

function fl_ClickToStopAllSounds(event:MouseEvent):void

{

    SoundMixer.stopAll();

}

thanks for your help

kglad
Adobe Expert
September 20, 2014

your 2nd code snippet is correct and it will not prevent any 'new' sound from playing.