Skip to main content
JamesM_
Participating Frequently
March 5, 2017
Answered

How do I make a toggle button to stop sound/music from playing?

  • March 5, 2017
  • 1 reply
  • 2206 views

I have researched on YouTube and through this support forum to try to understand how to do this, but I cannot get it to work. Does anyone know of a way to do this with actionscript, perhaps in the most simple form possible please?

This topic has been closed for replies.
Correct answer kglad

var s:Sound = new S();  // where S is the class (=linkage id in library panel) of your sound

var sc:SoundChannel = s.play();

var s_bool:Boolean = true;

toggle_button.addEventListener(MouseEvent.CLICK,toggle_soundF);

function toggle_soundF(e:MouseEvent):void{

if(s_bool){

sc.stop();

} else {

sc=s.play();

}

s_bool=!s_bool;

}

1 reply

kglad
kgladCorrect answer
Community Expert
March 5, 2017

var s:Sound = new S();  // where S is the class (=linkage id in library panel) of your sound

var sc:SoundChannel = s.play();

var s_bool:Boolean = true;

toggle_button.addEventListener(MouseEvent.CLICK,toggle_soundF);

function toggle_soundF(e:MouseEvent):void{

if(s_bool){

sc.stop();

} else {

sc=s.play();

}

s_bool=!s_bool;

}

JamesM_
JamesM_Author
Participating Frequently
March 5, 2017

Apologies, but I get the message "Call to a possibly undefined method s". Its refering to the part where it says new S.

I put the song name like the following:

var s:Sound = new S("Motivator.mp3");

I assume I am doing something incorrectly?

Colin Holgate
Inspiring
March 5, 2017

Is there any text in the Linkage column in the library? If there is, it's quite likely you put Motivator in there. If you did, the line should read:

var s:Sound = new Motivator();