You must be logged in to post Login Register
Search 

Edit Code for Sound Toggle Button

User Post

12:38 am
August 19, 2010


JMasterJ

New Member

posts 2

1

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;
}
}

10:21 pm
August 19, 2010


JMasterJ

New Member

posts 2

2

Actually I got it working to pretty much what I want a different way, except that my sound clip is about 10 seconds short of the animation… not s big deal but if i wanted to loop or repeat the track UNTIL the end of the animation and then start all ovr when it goes to frame 1, how would I do that?  I got it to loop but it continues to play at the beginning of a new animation loop and thus, causes two audio pieces to play at the same time.  I did that by changing the line

aChannel=aSound.play();

to aChannel=aSound.play(1,2);

which I think loops/repeats it one more time after the first.  Thanks.  But I need it to stop at the end of the last frame.  Thanks!
_____________________________________________

var aSound:Sound = new MySound();

var aChannel:SoundChannel = new SoundChannel();
aChannel=aSound.play();

toggle_btn.gotoAndStop(2);
toggle_btn.buttonMode = true;
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);

function togglePlay(event:MouseEvent):void {
     
      if (toggle_btn.currentFrame == 1) {
            aChannel=aSound.play();
            toggle_btn.gotoAndStop(2);
      } else {
            aChannel.stop();
            toggle_btn.gotoAndStop(1);
      }
}
_____________________________________________

Topic RSS 
Search