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!

Flash Audio: Creating Stereo Sound

Welcome back to our series about Flash Audio. Now we will deal with some more complicated stuff, stereo and mono sounds. To accomplish that, I have to instroduce you to the Transform object in Flash. Sounds complicated, but it’s not that complicated. In ActionScript, you have the “Object” DataType. Like this:

//define an object in ActionScript
var myObject:Object = new Object();

//add properties
myObject.name = “SomeBody”;
myObject.surname = “SomeSurname”;

Now you have an object with the properties name and surname.

But what has this to to with the sound? Here is the answer. The sound in Flash is transformed eg. divided into speakers. When the stereo sound is on (by default it is stereo), then the right speaker has the percentage of 100 and the left too. In ActionScript, it is written like this:

ll = 100
lr = 0
rr = 100
rl = 0

What the heck? It’s not that complicated. Ll stands for Left input on left speaker. Lr stands for left input on right speaker. Rr for right input on right speaker and rl for right input on left speaker. The variables can have values from 0 – 100. As you suggest, you adjust the values to create mono or stereo sound.

//sample mono on left speaker
ll = 100
lr = 100
rr = 0
rl = 0

//sample mono on right speaker
ll = 0
lr = 0
rr = 100
rl = 100

So now that you understand the show transform object you have to know how to apply this to the sound object. Assume you have a track in your Flash document library with the linkage ID set to “myTrack”. The code would look similar to this:

//create the new sound object
var mySound:Sound = new Sound();

//attach the track from the library
mySound.attachAudio(“myTrack”);

//create the mono sound transform object for the right speaker
var monoSoundRight:Object = new Object();
monoSoundRight.ll = 0
monoSoundRight.lr = 0
monoSoundRight.rr = 100
monoSoundRight.rl = 100

//apply it to the sound object
mySound.setTransform(monoSoundRight);

So, it is now very easy to create a Flash movie with buttons that switch between left and right speakers. Here is how it is done:

Open a new Flash document. Import a new sound into the library (Image 1).

Stereo audio in Flash

Give it the linkage ID “myTrack” (Image 2).

flash stero audio tutorial

Create two layers on the main timeline: “Actions” and “Content”. Make sure the “Actions” layer is above the “Content” layer (Image 3).

stero audio in flash

Create two buttons for the right and the left speaker. Place them on the stage and give the left button the instance name “left_btn” and “right_btn” to the right button (image 4).

flash audio tutorial

Paste the following code into the first frame of the Actions folder:

var mySound:Sound = new Sound();
mySound.attachSound(“myTrack”);

mySound.start(0, 50);

right_btn.onRelease = function () {
mySound.setTransform(rightMonoSound);
}

left_btn.onRelease = function () {
mySound.setTransform(leftMonoSound);
}

var leftMonoSound:Object = new Object();
leftMonoSound.ll = 100;
leftMonoSound.lr = 100;
leftMonoSound.rr = 0;
leftMonoSound.rl = 0;

var rightMonoSound:Object = new Object();
rightMonoSound.ll = 0;
rightMonoSound.lr = 0;
rightMonoSound.rr = 100;
rightMonoSound.rl = 100;

var stereoSound:Object = new Object();
rightMonoSound.ll = 100;
rightMonoSound.lr = 0;
rightMonoSound.rr = 100;
rightMonoSound.rl = 0;

Less Stress Now: Relax FAST at the Speed of Life

Less Stress Now: Relax FAST at the Speed of Life by Ronald Nathan, Ph.D.
Can your stress wait for you to jog, meditate or talk with a friend?
Over 60 percent of visits to doctors in this country are stress-related. It attacks not only our health, but our happiness, as well. Stress is the body\’s preparation to fight or flee danger. It was important for prehistoric survival, but today we have few physical fights and almost nowhere to flee. Instead we clamp a lid on the stress response, building up frustration, muscle tension and dangerous chemicals. Current remedies for stress relief are great if we have the time to use them. Exercise burns off the by-products of stress, yoga relaxes our bodies, and talking with a friend calms us down. But not having enough time to take care of ourselves is part of the stressful life most of us live. Is there a way to keep stress from building up by relaxing quickly throughout even our busiest days? Most of us tune out our bodies and push though the day. People may tell us we need to slow down and relax, but seldom does anyone tell us how. WELCOME TO BELLY BREATHING If you are new to belly breathing or need a refresher course, here it is. There is power in the simplicity of slow, belly breathing for stress relief and in the wonderful domino effect it has on the body. Yes, we all have a “fight-or-flight,” “wear-and-tear” emergency response, but we also have a “stay-and-play, “rest-and-digest\” relaxation/recuperative response. Just as stress and shallow breathing race the heart, raise our blood pressure, send butterflies into our stomachs, and tense up our muscles, relaxation and belly breathing reverse these unhealthy and unpleasant strains on our bodies. To learn belly breathing for stress relief, put your hand on your belly button. When you breathe out, push your hand in. When you breathe in, push your hand out. To deepen and slow your breathing, squeeze a little more out, so you’ll be able to take a little more in. Count how often you breathe. Most of us breathe 12-20 times a minute. Try for 10 or less during belly breathing. If you want to be sure you are belly breathing rather than chest breathing, get down on your hands and knees. Breathe. This posture forces you to belly breathe because it holds the chest at rest. Of course, once you learn belly breathing, you can do it without putting your hand on your stomach, counting or getting on the floor. You can belly breathe almost anywhere and anytime you want to relax. Start while you are waiting. The average American waits 40 frustrating minutes a day. We wait for red lights, elevators and cashiers. Callers put us on hold. Now you can give yourself a healthy break for stress relief. As you get good at belly breathing, you can also use it when you are upset. To listen to short samples of Dr. Nathan’s relaxation training recording, The FAST Technique for Stress Relief, go to www.SampleRelaxation.com or www.FreeRelaxation.com For a free six-minute sample, register and download it. © 2012 by Ronald Nathan, Ph.D.