Recipe 15.2. Starting and Stopping a Sound


Problem

You've created a Sound object and started streaming a sound file into it. Now you want to play it and eventually stop it.

Solution

Use the play( ) method to start the sound playing. Use the close( ) method to stop the sound from streaming.

Discussion

Playing a sound which has been loaded into a Sound object is very easy. Simply call the play( ) method of that Sound object, like so:

_sound = new Sound(new URLRequest("song.mp3")); _sound.play(  );

It is that simple. There are some additional optional parameters to the play( ) method, which are covered in Recipes 15.1 and 15.10.

The close( ) method of the Sound object not only stops the sound from playing, but also stops the streaming of the sound file. To play that sound again, call the load( ) method to restart loading the sound data. The close( ) method should be called only when you are sure you are finished with that particular sound. Recipe 15.1 discusses a way to stop the sound from playing without stopping the stream, using the SoundChannel class.

See Also

Recipe 15.1 for information on how to load external sound files and Recipe 15.10.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net