Controlling Sound Part 1
Created By
Benjamin Mace, at
2/11/2006 -
0 comments.
Many designers out there have an image in their head of a large recording studio and hundreds of adjustment and mixing knobs everywhere when thinking about sound manipulation. Controlling sound through Flash is fairly simple and it really hasn't changed much since the Sound Class was introduced in Flash 5.
We haven't done a lot of articles dealing with sound here at Actionscript.com so I thought I'd lay some ground work for everyone. This first article focuses on creating the sound and playing and stopping it with ActionScript. You should have a basic handle on ActionScript, importing assets into Flash and some knowledge of attaching symbols from the library to the timeline via linkage before diving into this article.
The Sound Object works the same as most of the built in objects shipped with Flash. It has several methods that are used to control things like volume and balance. In addition to setting properties like the volume, Flash can get properties like the volume and the current position of the playing sound. Here is a list from the Flash 8 Reference:
Sound([target:Object])
Creates a new Sound object for a specified movie clip.
attachSound(id:String) : Void
Attaches the sound specified in the id parameter to the specified Sound object.
getBytesLoaded() : Number
Returns the number of bytes loaded (streamed) for the specified Sound object.
getBytesTotal() : Number
Returns the size, in bytes, of the specified Sound object.
getPan() : Number
Returns the pan level set in the last setPan() call as an integer from -100 (left) to +100 (right).
getTransform() : Object
Returns the sound transform information for the specified Sound object set with the last Sound.setTransform() call.
getVolume() : Number
Returns the sound volume level as an integer from 0 to 100, where 0 is off and 100 is full volume.
loadSound(url:String, isStreaming:Boolean) : Void
Loads an MP3 file into a Sound object.
onLoad = function(success:Boolean) {}
Invoked automatically when a sound loads.
setPan(value:Number) : Void
Determines how the sound is played in the left and right channels (speakers).
setTransform(transformObject:Object) : Void
Sets the sound transform (or balance) information, for a Sound object.
setVolume(value:Number) : Void
Sets the volume for the Sound object.
start([secondOffset:Number], [loops:Number]) : Void
Starts playing the last attached sound from the beginning if no parameter is specified, or starting at the point in the sound specified by the secondOffset parameter.
stop([linkageID:String]) : Void
Stops all sounds currently playing if no parameter is specified, or just the sound specified in the idName parameter.
duration:Number [read-only]
The duration of a sound, in milliseconds.
id3:Object [read-only]
Provides access to the metadata that is part of an MP3 file.
position:Number [read-only]
The number of milliseconds a sound has been playing.
onID3 = function() {}
Invoked each time new ID3 data is available for an MP3 file that you load using Sound.attachSound() or Sound.loadSound().
onSoundComplete = function() {}
Invoked automatically when a sound finishes playing.
Let's get started. I've included a sample file with a loop in it I grabbed off of FlashKit.com here. This loop was created by noption @ www.technmedia.com. It's freeware and you can find hundreds of other music loops at FlashKit.com. Start by importing that MP3 file into your new file's library. Note that when importing sound formats you have several other options to MP3 files such as WAV and AIFF.
After you import the file, you'll want to pull up the linkage menu by right clicking on it in the library (option click for you single button mouse types) and give the sound a linkage identifier for ActionScript to reference it by. In the included single_sound sample file I've identified it as "loopSample". Make sure the checkboxes for export for ActionScript and export in first frame are checked as well. Now let's write some code in frame 1 of the main timeline:
1) myLoop = new Sound();
2) myLoop.attachSound("loopSample");
3) myLoop.start(0,10);
In line 1 I'm simply declaring a new sound object just like you would with a new movieclip or new array. Here I'm naming it "myLoop". You have the option to assign the sound object to a movieclip but we won't cover that yet.
Line 2 attaches the loop we imported to the sound object we just created on the stage. We use the attachSound method in the same manner we use the attachMovieClip method with movieclips. Note that our identifier is in quotes and is referenced as a string. You can also reference variables if you wanted to setup a function for example and pass in an identifier as a parameter.
Finally line 3 plays our sound. We use the start method to do this which has two parameters. The first is the offset for the sound which is set in seconds. If you wanted it to start 5 seconds in you would simply place 5 here.
The second parameter is the number of times you want the sound to play or the number of times it will loop. Here I'm setting it to 10 times. It's important to note that if you start your sound with an offset of 5 seconds and set your sound to loop 10 times, each loop will be offset 5 seconds.
Once you've started a sound, at some point you'll want to stop it. To stop the sound (regardless of loops or anything else) all you have to do is call the following for the sound object you created:
myLoop.stop();
Take scope into consideration when placing stop and start calls in buttons or other timelines. Unless you place you sound object in the _global scope you'll need to target the object from other timelines.
Now that you have a basic understanding of attaching, starting and stopping sound, try setting up a few different sounds in a single file and creating buttons to start and stop each. I've included a sample FLA with other loops by noption showing how you can control them if you need some help.
Recent Articles by
Benjamin Mace
Reader Comments