Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 21 ■ Questions and answers: Finishing the setup Methods and digital audio


There is only one AudioClip constructor method, which takes a source URL and uses this following
format :


AudioClip(String sourceURL)


Next, let’s take a look at the methods that the AudioClip class allows us to call off your AudioClip
objects. The DoubleProperty balanceProperty() method call allows you to obtain the relative left and right
volume levels for an AudioClip object. The IntegerProperty cycleCountProperty() method call allows you
to obtain the number of times the AudioClip object will be played when the play() method is called. The
DoubleProperty panProperty() method call allows you to obtain the (relative) center for an AudioClip
object. The IntegerProperty priorityProperty() method call allows you to obtain the (relative) priority
setting of that AudioClip object with respect to other AudioClip objects. The DoubleProperty rateProperty()
method call allows you to obtain a (relative) rate of speed at which that AudioClip is being played. The
DoubleProperty volumeProperty() method call allows you to obtain the (relative) volume level at which the
AudioClip is played.
Besides the six AudioClip Property methods, there are seven get() methods that allow you to get the
value of the AudioClip properties, including its digital audio source file. The double getBalance() method
call would be used to get the default balance level for the AudioClip. The int getCycleCount() method call
would be used to get the default cycle count for the AudioClip object. The double getPan() method call
would be used to get the default pan value for the AudioClip object. The int getPriority() method call
would be used to get a default playback priority value for the AudioClip object. The double getRate()
method call would be used to get the default playback rate for the AudioClip object. The String getSource()
method call would be used to get the source URL used to create the AudioClip object. The double
getVolume() method call would be used to get the default volume level for the AudioClip object.
There are also seven set() methods that allow you to set the value of the AudioClip properties, including
the digital audio source file. The void setBalance(double balance) method call should be used to set the
default balance level for the AudioClip object. The void setCycleCount(int count) method call should
be used to set the default cycle count for the AudioClip object. The void setPan(double pan) method call
should be used to set the default pan value for the AudioClip object.
The void setPriority(int priority) method call should be used to set the default playback priority.
The void setRate(double rate) method call should be used to set a default playback rate. The void
setVolume(double value) method call should be used to set the default volume level. There are also five
methods, which can be used to control the AudioClip object while it is playing.
The boolean isPlaying() method call will be used to indicate whether an AudioClip is currently playing.
The void play() method call should be used to play the AudioClip object using its default parameters. A void
play(double volume) method call should be used to play the AudioClip using all the default parameters
except for the volume. The void play(double volume, double balance, double rate, double pan, int
priority) method call should be used to play the AudioClip using the given volume, balance, rate, pan, and
priority parameters. Finally, the void stop() method call should be used to immediately stop all playback
of an AudioClip object. Now we can implement digital audio assets as AudioClip objects in our game to
provide audio sound effects for things such as gameboard spin and camera zooming.


Implementing AudioClip: Add Digital Audio Asset Sound Effects


The first thing that we will need to do is to declare two AudioClip objects named spinnerAudio and
cameraAudio at the top of the JavaFXGame class, as shown in the following Java 9 code and shown
highlighted in Figure 21-22:


AudioClip spinnerAudio, cameraAudio;

Free download pdf