# Edit Code for Sound Toggle Button

**URL:** https://forum.kirupa.com/t/edit-code-for-sound-toggle-button/313075
**Category:** flash
**Created:** [August 19, 2010, 7:37am UTC](https://forum.kirupa.com/t/edit-code-for-sound-toggle-button/313075 "2010-08-19T07:37:57Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![JMasterJ](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@JMasterJ](https://forum.kirupa.com/u/JMasterJ)
#### Post date: [August 19, 2010, 7:37am UTC](https://forum.kirupa.com/t/edit-code-for-sound-toggle-button/313075/1 "2010-08-19T07:37:57Z")

</div>

Hi all… this is the one code that I could make work, but I need it so the music plays at start and then the button will pause/stop it, and then turn into a play graphic which, when pressed, will play it again. I tried editing the code, but I am horrible at programming and of course nothing I tried worked. Please let me know what I have to change to start with music playing, and then have it stop/pause on first press, and alternate “play” and “stop” on subsequent presses. Thanks!

[My fla has 2 layers, one for this AS, and one for the buttons… within the symbol section, I have two layers, one for the button common BG, and two frames in the button layer, one for play (triangle) and one for stop (square). They are saved as a movie clip.]

If need be the tutorial for this button is at (remove all the “$” signs and spaces… I do not have enough posts yet to post urls)

flashperfection. c $o$m/$tutorials/$Toggle-sound-button-in-Actionscript-3-18231. h $t$m $l

THANKS!

//This stops the toggle buttons from playing the second frame.  
toggle\_btn.stop();

//Variable to detect whether the number of times the toggle button has been clicked.  
var clickOnce:uint=0;

//Creates a new instance of the imported sound.  
var aSound:Sound = new MySound();

//Create a new instance of the SoundChannel  
var aChannel:SoundChannel = new SoundChannel();

//This adds the mouse click event to the toggle button.  
toggle\_btn.addEventListener(MouseEvent.CLICK, togglePlay);

//If the toggle button has been clicked once then the sound plays  
//with an image of a stop symbol. If the toggle button is clicked  
//again then sound stops and the play symbol is displayed.  
function togglePlay(event:MouseEvent):void {  
clickOnce++;  
if (clickOnce==1) {  
aChannel=aSound.play();  
toggle\_btn.gotoAndStop(2);  
}  
if (clickOnce==2) {  
SoundMixer.stopAll();  
toggle\_btn.gotoAndStop(1);  
clickOnce=0;  
}  
}
