Sound with ActionScript


Although we have not covered ActionScript much thus far, it is important to understand some of the basic ideas and uses of the Sound object, one of the many native objects in ActionScript, which will be covered in more detail later in Chapter 8 and beyond.

Before you can use the Sound object, it must be initialized in the Actions panel, like this:

 var mySound_sound:Sound = new Sound(); 

After you have a Sound object initialized, several methods and properties can be called to manipulate this sound. But the first thing you want to do with a new Sound object is to get the sound into it. There are two methods for doing this, the loadSound() method and the attachSound() method. Because ActionScript is still new in the book, we will focus on the simpler method, the attachSound() method, which will grab the sound file from the library after it has been imported and you have set the linkage property.

In the following example, you are going to import an MP3 song, set its linkage property, and use ActionScript to start and stop it.

1.

Start by creating a new Flash document.

2.

Select File, Import, Import to Library, and then choose your favorite MP3 song or use the one from the accompanying website.

3.

After it is imported, go to the library, select the song you just imported, choose the panel Properties button from the top right of the library and select Linkage from the drop-down.

4.

Select the Export for ActionScript check box, and give it a linkage name of sample.

5.

Next, you'll want to add Start and Stop buttons to the stage. You can get them from the Common Libraries or create your own; just make sure they each have the instance property names of play_btn and stop_btn, respectively.

6.

Create a new layer and name it actions; then open the Actions panel in the first frame of this new layer (F9) and place the following actions in it:

 //create the new sound object var mySound_sound:Sound = new Sound(); //attach the song we want to this sound object mySound_sound.attachSound("sample"); //when released, the song will play play_btn.onRelease=function(){     mySound_sound.start(0,0); } //when released, the sound will stop stop_btn.onRelease=function(){     mySound_sound.stop(); } 

Now you can test the movie, and when you click the Play button, the song will begin to play. When you click the Stop button, the song will stop.

That was a simple way to make Start and Stop buttons with sound. Another great thing you can do with the sound object is to control the volume and pan of the sound itself. Pan is the left and right speaker control; basically, you can use the pan controls to make a sound appear to be coming more from the left or right speaker.

Building on the last example, you are going to make the sound automatically play, but this time, as it starts playing, it will fade out and to the left (if you have stereo speakers).

Simply replace the code in the Actions layer with this code:

 //create the new sound object var mySound_sound:Sound = new Sound(); //attach the song we want to this sound object mySound_sound.attachSound("sample"); //start the sound mySound_sound.start(0,0); //set the initial pan mySound_sound.setPan(100); //this function will fade it from the left to the right, and the volume to zero this.onEnterFrame=function(){     if(mySound_sound.getVolume()>0){         mySound_sound.setVolume(mySound_sound.getVolume()-.5);         mySound_sound.setPan(mySound_sound.getPan()-3);     } } 

Now when you test the movie, the sound automatically plays, and as it does, it fades out gradually from left to right.

You can do more things with the Sound object, such as get the current position in a song, as well as ID3 information about a song, which is included in most MP3s. You can also capture the event when the sound has completely finished playing. Just add the following code to the previous example, and you will see something in the Output panel when the song is done.

 mySound_sound.onSoundComplete = function(){     trace("All Done"); } 

But sound is not the only way to add a better experience to your websitethere is also video.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net