Monday, 17 May 2010

Toggle sound button in AS3 part 2

I previously wrote a Toggle sound button tutorial where you could play and stop an audio file using the same button. However, when you select the stop button it returns the audio back to the beginning. So, in this tutorial I will create a toggle sound button that will pause and continue from its current position. The Sound class in Actionscript 3 does not have a pause method, so you need to use the position property from the Sound Channel class. The position property stores the current point of the sound file which can be used to play the last stored point.


Toggle sound button in AS3 part 2

Step 1

Follow the Toggle sound button tutorial up to step 3 and create a pause button symbol instead of a stop button.


Step 2

Open up the Actions panel and add in the following code. Note I have left most of the code out from the previous tutorial you will need to add this in.

//This stores the current position of the played sound.
var pausePostion:Number = aChannel.position;

//If the toggle button has been clicked once then the sound plays
//with an image of a pause 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(pausePostion);
toggle_btn.gotoAndStop(2);
}
if (clickOnce==2) {
toggle_btn.gotoAndStop(1);
clickOnce=0;
aChannel.stop();
}
}

If you wish to return the pause symbol back to play symbol when the sound file has finished playing then you can used the sound complete event shown in the previous tutorial.


Step 3

Test your movie clip Ctrl + Enter. You should now have a toggle sound button that can pause in Actionscript 3.0.

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP