Flash provides excellent features for sound and making your application interactive.Sounds can be imported into the flash document and use it multiple times the way you want it.Flash supports .wav and .mp3 formats by default and other formats on having a compression application.Check the remaining formats by searching "Importing Sounds" in the Flash Help.
If you want to add the sound using GUI read the following.If you want to add the sounds using actionscript for more control read importing sound,adding identifier and jump directly to the actionscript at the end.
Importing Sounds
To import a sound into the document library navigate File > Import > Import to Library and select the file.
Adding Identifiers
Identifier is similar to the instance name of a movie clip.We use an identifier to refer the sound.Add identifier by Right-clicking the sound clip in the library panel and select Linkage in the menu.The linkage properties are also found in the properties box Advanced Menu.
In the linkage properties give an identifier name and make sure you have checked the Export for ActionScript and Export for first frame options.This is important for any sound to play.Give an identifier name to the sound.In this case I have named it 'smash'.
Adding Sound to the timeline
Like MovieClips or Buttons,sounds can also simply be dragged into the timeline.When a movie clipped is dragged into an empty frame,we can see a keyframe there.For sounds we see the wave of the sound clip.
Sound clip added this way always plays whenever the first frame of this sound wave is played in the original movie.This type of sound addition is suitable for background music that plays continuously through out the movie.
Add Sound to buttons
Sounds can be added to button instances by editing the button behaviors.Select the button for which the sound is to be added and Navigate to Window>Behaviors or press (Shift + F3) for the behaviours panel to show.Make sure that the button is selected while adding the behavior.We can see that in the Behavior panel too.Click the add behaviour button>Sound>Load Sound from Library.
Give the identifier of the sound to be added and also an instance name for the sound.
And added behaviour looks like this.
While the default behaviour is set to onRelease,we can choose other options too.
We can add sound directly in the button's timeline by dragging it.We can change the behaviour by placing it either in the Up,Over or Down keyframe similar to adding behaviour to the button for onRollOut, onRollOver or onRelease actions respectively.
Add and Control sounds using ActionScript
ActionScript has the Sound class for controlling sounds.We declare a new variable of type Sound.
var myClip:Sound=new Sound();
with the identifier added to the sound clip in the library,we link the identifier to our new sound object
myClip.attachSound("smash");
With this we have added a new Sound object and linked the sound clip to this object myClip.
We have full control of the myClip object with the following properties.
myClip.start() //Starts the sound
myClip.stop() //Stops the sound
myClip.getVolume() //Returns the sound volume level as an integer from 0 to 100
myClip.setVolume(value:Number) //Sets the sound volume level to the specified number
myClip.onSoundComplete= function() //Calls the function when the clip has competed playing
We can also get the attributes of the myClip object with the following.
myClip.duration //Returns the total duration of a sound, in milliseconds
myClip.position //Returns the duration after which the sound started playing.
The following code will start the sound at the beginning and stops when the button is clicked.
var myClip:Sound = new Sound();
myClip.attachSound("smash");
myClip.start() ;
stop_btn.onRelease = function() {
myClip.stop();
}
I am a blogger and Flash Develper.You can ask me to write tutorials on any topic you want.
Adding sounds in Flash
actionscript, adding sound, Sound class, Sound() | Thursday, June 18, 2009
This entry was posted on 3:44 PM
and is filed under
actionscript
,
adding sound
,
Sound class
,
Sound()
.
You can follow any responses to this entry through
the RSS 2.0 feed.
You can
View Comments
,
or trackback from your own site.
blog comments powered by Disqus
Subscribe to:
Post Comments (Atom)