Flash Audio: Pausing Sounds in Flash

This tutorial will show you how to create a pause button for the music loops you have added in the first tutorial.
This tutorial is built on top of the the tutorial ‘Creating a Sound On/Off Buttons’. You should complete the tutorial successfuly. Click here to open the tutorial. When you complete it you can come back here. Open the library from this tutorial and double click the button in library. Rename the button the ‘PauseSoundButton’ (Image 1).

creating a pause button for ausio in flash

Right click on the button and click ‘Edit…’. Remove the speaker graphic and add a simple text like ‘Pause Sound’ (Image 2).

creating a pause button for audio in flash

It’s noting spectacular as you can see, but up to you how you create your graphics. You can add what you want into the button.

As left from the previous tutorial, the button should be placed in the upper  right corner. Now click on the first frame of the ‘Actions’ layer, press F9 to open up the Actions panel. Remove the code from the previous tutorial and add the following code:

var mySound:Sound = new Sound();
mySound.attachSound("myTrack");
mySound.start();
var soundStarted:Boolean = true;
var lastPosition:Number = 0;
sound_btn.onRelease = function() {
    soundStarted = !soundStarted;
    if (soundStarted) {
        this._alpha = 100;
        mySound.start(lastPosition);
        //mySound.
    } else {
        this._alpha = 30;
        mySound.stop();
        lastPosition = Math.floor(mySound.position / 1000);
    }
};

That’s it! You have a functional pause button!