Skip to main content
September 22, 2019
Question

Music buttons - clicking on another music button will stop the music from the one you pressed before

  • September 22, 2019
  • 1 reply
  • 188 views

I have a few buttons that if clicked with mouse it will play music, I'm trying to get to where if I play another music button, it will stop the previous music. And if I play another music button it will stop the previous music, etc. If there's a ActionScript code for this, let me know

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
September 22, 2019

you can always use SoundMixer.stopAllSounds() to stop all currently playing sounds, though it's often better to use a variable to reference the last played soundchannel and stop it explicitly when playing a new sound.  eg,

 

var currentSoundChannel;

 

function button1F(e:MouseEvent):void{

stopCurrentSoundF();

var s1:Sound1 = new Sound1();

currentSoundChannel = s1.play();

}

function button2F(e:MouseEvent):void{

stopCurrentSoundF();

var s2:Sound2 = new Sound2();

currentSoundChannel = s2.play();

}

etc...

 

function stopCurrentSoundF():void{

if(currentSoundChannel){

currentSoundChannel.stop();

}

}