Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Stop sound when another sound plays

New Here ,
Sep 19, 2014 Sep 19, 2014

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.

TOPICS
ActionScript
8.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 21, 2014 Sep 21, 2014

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();

}

Translate
Community Expert ,
Sep 20, 2014 Sep 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 20, 2014 Sep 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2014 Sep 20, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 20, 2014 Sep 20, 2014

Thanks, its still not working, maybe I am putting the code in the wrong spot.

Can you look at my fla file? Right now I added 2 sound buttons, and 3rd button is a "stop all sounds button" which does work correctly. Can you tell me where to add the code to do what I wanted with stopping the other sounds? Thank you.

Here is the link:

https://www.mediafire.com/?7t5jqew3f8yp1uz

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 20, 2014 Sep 20, 2014

i don't download and correct files unless i'm hired.

this is simple enough that it should be solvable in the adobe forums without the need to download your files.

create a new 1 frame fla, put all 3 buttons in frame 1 and all the code in frame 1.  then copy and paste your code to this forum if there's a problem.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 20, 2014 Sep 20, 2014

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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 21, 2014 Sep 21, 2014

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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 27, 2014 Sep 27, 2014

Now that I corrected my code thanks to Ned M., this part is the correct answer, thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2018 May 29, 2018
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines