Wednesday, August 5, 2009

Playing Sound in Flash Part 1

You all know that you can play sound in Flash.
This post will explore the various ways you can play sound in Flash.
Basically, we can break it down to 3 different techniques:

A: Import the sound into Flash and associate that sound to a keyframe (by selecting a keyframe and then choosing the sound from the "sound" area of the Property panel) when the playhead in the timeline reaches that keyframe, the sound will play.

B: Import the sound into Flash and use Actionscript to play the sound. Note in this case the sound is never associated with a keyframe rather you can play the sound whenever you need to by writing some Actionscript, playing the sound is not dependant on the playhead reaching a particular frame.

C. Keep the sound external to Flash (and the swf file) and use Actionscript to load and play the file. (We will cover details of this technique in another upcoming post.)

The remainder of this post we'll cover details of technique "B" above, using Actionscript to play a sound that's been imported into the Library.

If you use this technique, there is one thing you need to do before writing the Actionscript and testing the movie. It involves the fact that if you have a resource (such as a sound file or graphic) in the library and you don't have a reference to that resource in the timeline, when you create an swf file, either when publishing or just testing the movie, Flash will assume that because there's no reference to that resource in the timeline that you don't actually need it, it assumes you're not actually using it and it throws away the data that represents that resource: why save data that's not being used, but of course you are using that resource, you're just using Actionscript to play it back and not the timeline. You need to tell Flash not to throw it away.

Here's what you do: in the Library panel, select the imported sound file and then right-mouse click on it. Choose "Linkage" and then when the dialogue box appears click on the check box that says "Export for Actionscript".



This is essentially telling Flash not to throw this resource away, you're going to be using it with Actionscript. Also I'd recommend, though technically it's not required, you can just use the name of the sound file without the extension, that you type in a new Class name, in the example above I typed in "mySound" (without the quotes). Note that his is an arbitrary word, you're making it up.

When you click Ok to close the dialogue box Flash will put up a message which says: "A definition for this class can not be found in the classpath, so one will be automatically generated..." Great, no problem. If you know what a class is fine, if not, don't worry about it. Though you will be referencing that class in the first line of code below. Flash is essentially creating a new sound class based on the particular sound name you entered.

After that you can start writing the Actionscript code that will play the sound.
To do so you will need to use the Sound class and create a new instance (object) from it.

To play the sound first use the class name you typed into the linkage dialogue box above and create a new instance of it using the first line of code below.

var myNewSound:Sound = new mySound();

Note, whatever name you used in the dialogue box for the class name is the word you need to use in the line above where I have "mySound". If you used pepperoniPizza as the class name in the dialogue box your code would look like this:

var myNewSound:Sound = new pepperoniPizza( );

The sound wont start playing till you referencre the instance of the sound object you created in the first line and execute the play method:

myNewSound.play();

To stop the sound from playing execute the stop method:

myNewSound.stop();

The completed code for playing the sound:

var myNewSound:Sound = new mySound();
myNewSound.play();

Your homework assignment is to take this code and modify it so that you play a sound when the user clicks on one button and stop the sound when the user clicks on another button.

Note: the screenshot above was taken from Flash CS3 but the technique is the same for Flash CS4 though the dialogue box there looks a bit different you still choose Linkage Export for Actionscript. Also, we're using Actionscript 3 code here which works in either Flash CS3 or Flash CS4.




No comments: