SIM1 - Class 11 - 2011

Subject SIM1 - Interactive Media 1

BSBINN502A.1 - Build and Sustain and Innovative Work Environment (part 1)
CUFDIG506A.1 - Design Interaction (part 1)

Teacher Lisa Cianci - email:
Class Website:
                         (username & password = student)

Flash and ActionScript

Today we will be covering the following topics:


ActionScript Sound Architecture

The ActionScript 3.0 sound scripting architecture is composed of several new classes that contribute to a finer degree of control over sound data and sound manipulation than previously available. Along with this control, however, comes an increased verbosity in sound-related scripts (more code!). Before moving on to specific examples, it will help to understand the primary purposes of the main sound classes we’ll be using.

The Sound class is the first stop in working with sound. It is used to load a sound, play a sound, and manage basic sound properties.

The SoundChannel class is used to create a separate channel for each new sound played. The use of the word channel, in this case, does not refer to the left and right channel of a stereo sound. Sounds in channels can be either mono or stereo. Instead, a channel created by the SoundChannel class is analogous to multitrack recording techniques. By placing each sound in its own channel, you can work with multiple sounds but control each sound separately.

As the name implies, the SoundMixer class creates a central mixer object through which all sound channels are mixed. Changes to the mixer will affect all playing sounds. For example, this class can be used to conveniently stop all sounds that are playing.

This class is used to control the volume and panning between left and right stereo channels of a source. It can be used to affect a single sound channel, a mixer object to globally affect all playing sounds, the microphone, and even the sound of a video.

The ID3Info class is used to retrieve metadata written into ID3 tags found in an MP3 file. ID3 tags are used to store information about the MP3, including artist name, song name, track number, and genre.

Other classes include SoundLoaderContext and Microphone, but we won't be covering these today.


Internal and External Sounds
Often, the use of sound in your projects will entail loading sounds from external sources. Keeping your sounds external to your primary SWF has many benefits. It can keep the file size of your SWF from becoming too large, and it’s easy to change the audio file without having to recompile your SWF, just to cite two examples.

It is possible to use internal sounds though, without having to rely on the timeline. Today, we’ll look at how to store a reference to both an internal and an external sound. These references can then be used to play the sound, create a sound channel, and otherwise manage and manipulate the sound in various ways.


When to use internal / external sounds?
Internal sounds are best used for short sounds - sound effects, button sounds etc. Also if you are animating on the frame and want to do lip-synching or synching of your sound to specific frames. Internal sounds load with the SWF so they are ready to use as soon as the SWF is loaded - no waiting around.

External sounds are best for long soundtracks and for sound players that have long tracks and offer the user a choice of track - this means the SWF file is much smaller, and the user just loads the track they want, when they want it. You can use a progress bar or text field to indicate the load progress of external sound files.


Working with Sounds in Your Library
Creating an instance of a sound from your library is the same as creating an instance of a movieclip symbol - after importing a sound, open the Symbol Properties or Linkage dialog, and click the Export for ActionScript check box. This will create a linkage identifier, in the form of a class name, by which you can refer to the sound in your code.

By default, the class name will (sometimes) be assigned automatically based on the name of the imported sound. This is important because the name of the sound may have a three-character extension, such as .mp3, or similar extensions for WAVE or AIFF files. You may also notice that, if your file name has spaces in it, the Flash interface will (sometimes) automatically remove the spaces. Neither spaces nor full-stops are allowed in class names, so you may need to adjust the identifier used. Following best practices, you may also wish to make sure the class name starts with a capitalized alpha character (rather than a numeral). For example if your sound file is called "mysong.mp3", then the class name should be changed to "Mysong" in the Linkage Class setting - removing both the full-stop and the extension, and making the first character Uppercase.

To create an instance of the library-bound sound called "Mysong", use this code:

var mysound:Sound = new Mysong();


Loading External Sounds
Simply loading an external sound requires a bit more than invoking the load() method. You must create a new sound instance. This process follows the same techniques we’ve used before to create new instances of other objects, as shown in line 1 of the following code:

var mysound:Sound = new Sound();
var myURL:URLRequest = new URLRequest("mysong.mp3");

The loading process shown in the load() line of code however, requires the use of a new class called URLRequest. It is important to note that this class standardizes the use of external URLs by passing on any information you may need to provide to the Flash Player.


Exercise 1 - internal sound

We will create a simple sound player using a sound from the Library. Create a new Flash file and import the 2 short wav files provided:

Create 2 buttons, or get 2 playback buttons from the buttons library - a play button and a stop button. Put two play buttons and one stop button on the stage. Give the buttons the following instance names:

Code for Exercise 1

Copy and paste this code on frame 1 of your flash file:

//sound instances from Library bound sounds
var mysound1:Sound = new Meow();
var mysound2:Sound = new Woof();
//sound channel instances
var channel1:SoundChannel;
var channel2:SoundChannel; //--------------------------------- //LISTENERS //--------------------------------- //button events btnSound1.addEventListener(MouseEvent.CLICK, sound1Handler); btnSound2.addEventListener(MouseEvent.CLICK, sound2Handler); btnStopAll.addEventListener(MouseEvent.CLICK, stopAllHandler); //--------------------------------- //FUNCTIONS //--------------------------------- function sound1Handler(e:MouseEvent):void {; } function sound2Handler(e:MouseEvent):void {; } function stopAllHandler(e:MouseEvent):void { SoundMixer.stopAll(); //to stop one sound channel: //channel1.stop(); }
Save and test your Flash file, and debug where necessary.


Exercise 2 - external sound

Create a sound player loading an external MP3 file. Using the same flash file as the previous exercise, make sure you have the following mp3 file in a "sounds" folder which is in the same folder as your Flash file:

You need a pause button, a play button and a stop button on the stage with the following instance names:

Create a dynamic text field on the stage with the following instance name:

Create a slider for volume control as follows...

Make a small circle movieclip symbol in the library - call it slider_mc - it should be about 10 - 15 pixels in diameter. Make sure the registration point is in the centre of the movieclip.

Create a movieclip in the library called volume_mc and place a horizontal line in the movieclip that is 100 pixels long with the registration point at the left edge of the line. This is important because we will calculate the volume of the sound accordng to where the slider is positioned along this line.

Place one instance of the slider_mc into the volume_mc movieclip at the right edge of the line - indicating 100% volume. Give the slider_mc instance the instance name "slider_mc".

Place one instance of the volume_mc movieclip onto the stage in the main timeline and give it the instance name "volume_mc".


Code for Exercise 2

//sound instance from external MP3 sound file
var mysound3:Sound = new Sound();
//sound channel instance
var channel3:SoundChannel;
//get external sound file
var myExtSound:URLRequest=new URLRequest("sounds/06 Little Trouble Girl.mp3");
//variable to hold pause position
var pausePos:Number=0;
//variable to detect if sound is playing or not
var isPlaying:Boolean=false;
//variables for volume slider
var soundVolume:Number;
var dragging:Boolean=false;
//draggable rectangle area for the slider 
var rectangle:Rectangle=new Rectangle(0,0,100,0);
//LOAD external sound
mysound3.addEventListener(ProgressEvent.PROGRESS, onLoadProgress, false, 0, true);
mysound3.addEventListener(Event.COMPLETE, onLoadComplete, false, 0, true);
//button events
btnPlay.addEventListener(MouseEvent.CLICK, playHandler);
btnStop.addEventListener(MouseEvent.CLICK, stopHandler);
btnPause.addEventListener(MouseEvent.CLICK, pauseHandler);
//volume control listeners
volume_mc.slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragIt);
//put the Mouse_Up listener onto the stage not the slider_mc to prevent 
// missing the stop drag if the mouse leaves the slider before letting go 
addEventListener(MouseEvent.MOUSE_UP, dropIt);
function onLoadProgress(e:ProgressEvent):void {
   //trace the progress to output panel - you can use this 
//to create a loader bar or test field
trace(int(100 * (e.bytesLoaded / e.bytesTotal))); } function onLoadComplete(e:Event):void { trace("The sound is completely loaded."); //get the ID3 data from the sound file var id3artist:String=mysound3.id3.artist; var id3songname:String=mysound3.id3.songName; var id3album:String=mysound3.id3.album; //put the ID3 data into a dynamic text field which is on the stage txtSoundInfo.text="song: "+id3songname+" \nartist: "+id3artist+" \nalbum: "+id3album; } function playHandler(e:MouseEvent):void { //if the sound is not playing, play from the pause position if (! isPlaying) {; isPlaying=true; trace("play"); //set the sound volume to the slider x position soundVolume=volume_mc.slider_mc.x/100; var st:SoundTransform=new SoundTransform(soundVolume); channel3.soundTransform=st; } } function stopHandler(e:MouseEvent):void { //if the sound is playing, stop it if (isPlaying) { channel3.stop(); isPlaying=false; pausePos=0; trace("stop"); } } function pauseHandler(e:Event):void { if (isPlaying) { pausePos=channel3.position; channel3.stop(); isPlaying=false; } } function dragIt(e:Event):void { volume_mc.slider_mc.startDrag(false, rectangle); dragging=true; volume_mc.slider_mc.addEventListener(Event.ENTER_FRAME, adjustVolume); } function dropIt(e:Event):void { if (dragging) { volume_mc.slider_mc.stopDrag(); volume_mc.slider_mc.removeEventListener(Event.ENTER_FRAME, adjustVolume); dragging=false; } } function adjustVolume(e:Event):void { soundVolume=volume_mc.slider_mc.x/100; var st:SoundTransform=new SoundTransform(soundVolume); if (channel3!=null) { channel3.soundTransform=st; } }

Save, test and debug

see the SWF
get the empty (no code) FLA


get all the files & code here for today's class.


Interesting References

here are a coupld of interesting sites I found in the last week. they relate to storytelling - they might not be of much use for your children's interactive stories, but concepts here will apply to many narratives.

The periodic table of storytelling

TV tropes wiki



Use any remaining time to work on your assessment tasks.