Adding Video and Sound - Using the Sound Class
(Page 5 of 9 )
The code generated by the Behaviors panel shows how Flash Pro uses the many different elements of the Sound class. Tables 10.3–10.5 summarize the methods, properties, and events, respectively, of the Sound class.
Table 10.3 Sound Class Methods
Method | Actions |
soundInst.attachSound("id") | Attaches the sound identified by the linkage name id to the instance. |
soundInst.getBytesLoaded() | Returns the number of bytes in the sound instance that's currently loaded. |
soundInst.getBytesTotal() | Returns the total number of bytes in the sound instance. |
soundInst.getPan() | Returns the current pan value established by setPan(). |
soundInst.getTransform() | Returns the value from the last setTransform() call. |
soundInst.getVolume() | Returns the current volume setting. |
soundInst.loadSound("url", Boolean) | Loads the MP3 file identified by url with the option of streaming (Boolean true) or event (Boolean false). |
soundInst.setPan(n) | The pan value (n) is set in terms of negative values up to –100 for the left speaker and positive values up to 100 for the right speaker. Mid balance is 0. |
soundInst.setTransform(Obj) | Establishes the amount of the two channels that will be played in left and right speakers. (Obj is an object containing the channel properties for left/right speakers.) |
soundInst.setVolume(n) | The volume (n) can be set from 0 to 100, where 0 indicates the sound is off. |
soundInst.start(s,l) | The sound begins playing with these optional parameters: start position in seconds (s) and number of times to loop (l). |
soundInst.stop() | Only stops the sound instance's sound. |
Table 10.4 Sound Class Properties
Property | Characteristics |
soundInst.duration | Returns the number of milliseconds in a sound instance. |
soundInst.id3.tagName | A read-only property to access different ID3 1.0/2.0 tags (tagName) in an MP3's metadata. |
soundInst.position | Returns the number of milliseconds for which a sound has been playing. |
Table 10.5 Sound Class Events
Event | Description |
soundInst.onID3 | Fires after the load sequence and ID3 data become available. |
soundInst.onLoad | When a sound is loaded, the event fires. |
soundInst.onSoundComplete | Fires once the sound has stopped playing. |
The Sound class provides two methods to access sound: attachSound() and loadSound(). When you use attachSound(), you need to first import a sound into the Library panel and give it a linkage name. Using loadSound(), you can load the sound file directly from the server, either as an event sound or a stream sound. When you select a stream sound, the sound begins playing shortly after the loading begins.
All instances of the Sound class must first be instantiated using the Sound constructor, such as in the following line:
var soundBarrier:Sound = new Sound();
Once the Sound class is instantiated, its different methods, properties, and events can be attached to the instance. Using the Sound class, you have far more control over sound than with any other technique available in Flash Pro.
This chapter is from Macromedia Flash MX Professional 2004, by Bill Sanders (Sams, 2004, ISBN: 0672326051). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Project: Experimenting with Sound >>
More Flash Articles
More By Bill Sanders