Category Archives: Flash Audio Tutorials

Flash Audio: Creating a Sound On/Off Button

This tutorial will teach you how to create a Sound On / Off button for the music loop you have just added in the first flash tutorial.
This tutorial is built on top of the the tutorial ‘How To Import And Add Sound To A Movie‘ that deals with the basics of importing sounds to Flash. This means that this tutorial assumes that you have a sound loop imported into the library of your movie. With the music loop in your library we can start building a button.

Go to Insert -> New Symbol or press Ctrl + F8 (Image 1).

on off audio button in flash

Ok, now the dialog box titled ‘Create New Symbol’ appears. Type in the name box ‘SoundOnOffButton’ for type select ‘Button’ and click ‘Ok’. Before clicking the Ok button, you button should look similar to image 2.

sound on off button in flash project

The button appears in the library and you should be now in the editing mode of the button. Take a look at the image 3 so will know what I’m talking about.

sound on off button in flash

Rename the layer to ‘Speaker Graphic’. As you can suggest, the layer  will display a graphic with a simple speaker. I used the line tool to draw something like this on the image 4. It’s not fancy, I know, but you get the idea.

sound on off button in flash audio project

With this button, you can go to the main timeline by clicking on the back button above the timeline of the button (Image 5).

sound on off button in flash audio project

On the main timeline add an additional layer ‘Actions’. Now you should have two layers in the main timeline. The upper one renamed ‘Actions’ and the layer below renamed to ‘Content’. Select the first frame of the layer ‘Content’ and drag the SoundOnOffButton from the library somewhere on the stage. In my sample I placed it on the upper right corner of the Stage (Image 6). When placed on the stage, in the Properties panel, give it an instance name of ‘sound_btn’. To do this, keep the button selected on the stage, open the properties panel (Window -> Properites) and type ‘sound_btn’ in the left text box where it says ‘<Instance Name>’. See image 6.

audio loops in flash on off button

The next step you should do is to add a linkage name to your imported sound loop. To do this, open up the library, right click the sound file in select ‘Linkage…’ (Image 7).

soud on off button in flash

The ‘Linkage Properties’ dialog box appears. Check the option ‘Export for ActionScript’ and in the ‘Identifier’ text box, type in ‘myTrack’. Make sure the dialog box looks similar to image 8 and then click Ok.

sound on off button in flash how to

Now select the first frame of the layer Actions and open up the Actions layer (press F9). Type in (or paste) the following lines of code code:

var mySound:Sound = new Sound();
mySound.attachSound("myTrack");
mySound.start();
var soundStarted:Boolean = true;
sound_btn.onRelease = function() {
    soundStarted = !soundStarted;
    if (soundStarted) {
        this._alpha = 100;
        mySound.start();
    } else {
        this._alpha = 30;
        mySound.stop();
    }
};

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!

Flash Audio: Sound Fade In / Fade Out

Assuming you have successfully completed the previous tutorials I expect that I do not have to show you how to create buttons, import sounds, add graphics to sounds etc. In this tutorial we need to concetrate on the ActionScript that fades in/out the played sound.

Follow these steps to do this:

1.    Import a sound loop to you Flash Movie
2.    Give it a linkage ID ‘myTrack’
3.    Create to buttons. One with a plus sign inside and the other one with a minus sign inside. Place the on the stage like on the image 1.

Flash tutorial

4.    The plus button should have the instance name ‘fadeIn_btn’, the minus button ‘fadeOut_btn’.

The button should be placed on the layer ‘Content’. Above this layer, create a layer called ‘Actions’, select the first frame and open the Actions Panel (press F9). Paste the following code:

var mySound:Sound = new Sound();
mySound.attachSound("myTrack");
mySound.start();
var soundStarted:Boolean = true;
var lastPosition:Number = 0;
var id:Number;
fadeIn_btn.onRelease = fadeInSound;
function fadeInSound():Void {
    if (id) {
        clearInterval(id);
    }
    mySound.start(lastPosition);
    var volume = mySound.getVolume();
    id = setInterval(fadeIn, 10);
    function fadeIn():Void {
        mySound.setVolume(volume++);
        if (volume>=100) {
            clearInterval(id);
        }
    }
}
fadeOut_btn.onRelease = fadeOutSound;
function fadeOutSound():Void {
    if (id) {
        clearInterval(id);
    }
    var volume = mySound.getVolume();
    id = setInterval(fadeOut, 10);
    function fadeOut():Void {
        mySound.setVolume(volume–);
        if (volume<=0) {
            clearInterval(id);
            mySound.stop();
            lastPosition = Math.round(mySound.position/1000);
        }
    }
}

Flash Audio: Adding Sound Loops

Adding sound loops to a Flash Movie is a simple task. The first thing you have to do is to find the appropriate loop for your Flash Movie. We have lots of free royalty free music loops on our home page at Partners In Rhyme.com that you can download and use with these tutorials.
You can add sound loops for your animations such as cartoons or intros. The whole thing is very easy. If you have a Flash movie with content, like on image 1.

Flash audio tutorial

To add a loop to your movie, follow these steps:

1.    Add an extra layer called “Sound”.
2.    Import a sound loop the you Flash Movie.
3.    Select the first frame of the layer
4.    In the properties panel, in the “Sound” drop down menu, select the imported sound.

That’s all, now you can specify the number of times to loop! On image 3, you can how the image should know look like.

adding music loops to flash

Flash Audio: Streaming MP3’s in Flash

This tutorial will shortly explain the importance of the loadSound method. This method is used when you want to load external MP3 files. Here is the syntax:
public loadSound(url:String, isStreaming:Boolean) : Void
Let’s see how it works!
As I already said, this loads an MP3 file into the Sound object you created. The parametar isStreaming is interesting. You can use it to indicate whether the sound is an event or a streaming sound.
Just like it is the case with every sound, event sounds must be completely loaded before they play. They are managed by the ActionScript Sound class and respond to all methods and properties of this class.
Streaming sounds play while they are downloading. Playback begins when sufficient data has been received to start the decompressor.
All MP3s (event or streaming) loaded with this method are saved in the browser’s file cache on the user’s system. Here is the overview of the two parameteres:
url:String – The location on a server of an MP3 sound file.
isStreaming:Boolean – A Boolean value that indicates whether the sound is a streaming sound (true) or an event sound (false).
Let’s create an example!
Open up a new Flash document. Place an input text field onto the stage (Image 1).
Streamign Audio on Flash

Give it an instance name songName_txt (Image 2).

Streaming MP3 audio in Flash

Add a simple button besides the text field and give it the instance name startSound_btn.(Image 3)

Load sound flash tutorial

Ok, now add an additional layer and name it „Actions“. Rename the layer with the text field at the botton to „Text Field“ (Image 4).

Make sure you have MP3 tracks in the same folder where the Flash movie resides.
Select the first frame of the layer Actions and add the following ActionScript:
var soundToLoad:Sound = new Sound();
startSound_btn.onRelease = function () {
    var song = songName_txt.text;
    soundToLoad.loadSound(song, true);
}
soundToLoad.onLoad = function () {
    this.start(0, 1);
}
Test the movie. Type in the name of one of your tracks and press the start button. The mp3 file should load and play!