13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with sound<br />

Know exactly wh<strong>en</strong> a sound finishes playing<br />

Track the playback progress of a sound<br />

Change volume or panning while a sound plays<br />

To perform these operations during playback, use the SoundChannel, SoundMixer, and SoundTransform classes.<br />

The SoundChannel class controls the playback of a single sound. The SoundChannel.position property can be<br />

thought of as a playhead, indicating the curr<strong>en</strong>t point in the sound data that’s being played.<br />

Wh<strong>en</strong> an application calls the Sound.play() method, a new instance of the SoundChannel class is created to control<br />

the playback.<br />

Your application can play a sound from a specific starting position by passing that position, in terms of milliseconds,<br />

as the startTime parameter of the Sound.play() method. It can also specify a fixed number of times to repeat the<br />

sound in rapid succession by passing a numeric value in the loops parameter of the Sound.play() method.<br />

Wh<strong>en</strong> the Sound.play() method is called with both a startTime parameter and a loops parameter, the sound is<br />

played back repeatedly from the same starting point each time, as shown in the following code:<br />

var snd:Sound = new Sound(new URLRequest("repeatingSound.mp3"));<br />

snd.play(1000, 3);<br />

In this example, the sound is played from a point one second after the start of the sound, three times in succession.<br />

Pausing and resuming a sound<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

If your application plays long sounds, like songs or podcasts, you probably want to let users pause and resume the<br />

playback of those sounds. A sound cannot literally be paused during playback in ActionScript; it can only be stopped.<br />

However, a sound can be played starting from any point. You can record the position of the sound at the time it was<br />

stopped, and th<strong>en</strong> replay the sound starting at that position later.<br />

For example, let’s say your code loads and plays a sound file like this:<br />

var snd:Sound = new Sound(new URLRequest("bigSound.mp3"));<br />

var channel:SoundChannel = snd.play();<br />

While the sound plays, the SoundChannel.position property indicates the point in the sound file that is curr<strong>en</strong>tly<br />

being played. Your application can store the position value before stopping the sound from playing, as follows:<br />

var pausePosition:int = channel.position;<br />

channel.stop();<br />

To resume playing the sound, pass the previously stored position value to restart the sound from the same point it<br />

stopped at before.<br />

channel = snd.play(pausePosition);<br />

Monitoring playback<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Your application might want to know wh<strong>en</strong> a sound stops playing so it can start playing another sound, or clean up<br />

some resources used during the previous playback. The SoundChannel class dispatches an Ev<strong>en</strong>t.SOUND_COMPLETE<br />

ev<strong>en</strong>t wh<strong>en</strong> its sound finishes playing. Your application can list<strong>en</strong> for this ev<strong>en</strong>t and take appropriate action, as shown<br />

below:<br />

Last updated 6/6/2012<br />

451

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!