Copy link to clipboard
Copied
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.
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();
}
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
your 2nd code snippet is correct and it will not prevent any 'new' sound from playing.
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
Now that I corrected my code thanks to Ned M., this part is the correct answer, thanks
Copy link to clipboard
Copied
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!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now