<< Chapter < Page Chapter >> Page >

Encapsulate and play the wind wound

Listing 4 uses similar code to encapsulate the contents of the file named wind.mp3 in an object of type Sound referred to by the instance variable named wind .

Then Listing 4 calls the play method on the wind object to cause the wind sound to be played from beginning to end twice when theprogram first starts running. (It will be played again later at random times.)

The play method of the Sound class

Here is part of what the documentation has to say about the play method of the Sound class:

"Generates a new SoundChannel object to play back the sound. This method returns a SoundChannel object, which you access to stop the soundand to monitor volume. (To control the volume, panning, and balance, access the SoundTransform object assigned to the sound channel.) "

In other words, the play method causes the sound to start playing through a SoundChannel object, which you can manipulate to achieve various effects.

Didn't save a reference to SoundChannel object

Because I didn't have any need to manipulate the wind sound by way of the SoundChannel object, I didn't capture and save a reference to the object returned by the play method.

Parameters of the play method of the Sound class

The play method has three parameters, each of which has a default value. By default (and this doesn't seem to agree with the documentation) , if you call the play method on a Sound object and don't pass any parameters, the sound encapsulated in the object will be played once, starting at the beginning of the sound.

The first parameter

The first parameter is the initial position in milliseconds at which playback should start. The default value for this parameter is 0, which causes the sound to start at the beginning by default.

As is always the case with default parameters, if you want to provide a non-default value for the second parameter, you must also provide a value forthe first parameter. When the play method is called on the wind sound in Listing 4, a value of 0 is passed as the first parameter to cause the sound to play from the beginning.

The second parameter

Instead of telling you what the documentation seems to say about the second parameter, I'm going to tell you how the second parameter behaves, which doesn'tseem to agree with the documentation.

The value of the second parameter defines the number of times the sound will be played before the sound channel stops playback. For example, a value of 2 ispassed as the second parameter when the play method is called on the wind sound in Listing 4. This parameter, in conjunction with the first parameter, causes the wind sound to be played through twice from the beginning to the end whenthe program starts running.

Rain, rain, go away: not any time soon

The maximum possible integer value is passed as the second parameter when the play method is called on the rain sound in Listing 4. This causes the rain sound to play over and over for a length of timethat is probably longer than anyone would want to listen to it.

A CREATION_COMPLETE event listener

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask