Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 21 ■ Questions and answers: Finishing the setup Methods and digital audio

To get some practice, create the other 59 sets of question answers in the setupQSgameplay() methods
now. Next, let’s put an AudioClip object (class) into place so we can attach sound effects to our game board
spin animation.


Digital Audio for Games: Using the AudioClip Class


Let’s also take a look at how to add digital audio assets to your games during this chapter. This will require
the use of the javafx.media module, which will make your distribution larger because this module will need
to be added to your distribution JAR and includes both the MediaPlayer (used for both audio and video)
and the AudioClip class, among others. The AudioClip class is used for shorter audio “snippets” technically
called samples. If you want to play longer format digital audio (say, songs) or digital video, you will want to
instead use the MediaPlayer class. Games usually use shorter format audio, and therefore we are going to
cover the AudioClip class here; it is essentially a digital audio sequencer, which is a very powerful tool, both
for game developers and for sound designers and songwriters.
The public final AudioClip class extends java.lang.Object, meaning it was scratch-coded to be a digital
audio sequencer. It is kept in the javafx.scene.media package in the javafx.media module, and thus, the
Java class hierarchy for the class looks like the following:


java.lang.Object



javafx.scene.media.AudioClip



An AudioClip object can be used to contain short segments of digital audio that will be played with
minimal latency. Clips are loaded from a network or JAR similarly to Media objects but have a different
behavior. For example, Media objects that are played by a MediaPlayer object cannot “play” themselves,
whereas your AudioClip objects can. AudioClips are immediately reusable, so they have zero latency and
use less memory, which is important for games.
The playback behavior of an AudioClip object is what Oracle calls “fire and forget.” Once one of the
class’s play() methods is called, the only operable control is the stop() method. We will be using both of these
methods.
An AudioClip object can also be played multiple times simultaneously! To accomplish this same task
using a Media object in a MediaPlayer, one would have to create new MediaPlayer objects for each sound
played in parallel. This is not optimal for gameplay scenarios, which is why we are not covering Media and
MediaPlayer objects here.
Media objects and MediaPlayer are better suited for long-format audio such as songs or audiobooks.
This is primarily because an AudioClip stores (in memory) a raw, uncompressed (PCM) audio data for the
entire digital audio assets, which is usually quite large for long audio clips. A MediaPlayer will only have
enough decompressed audio data “prerolled” in memory to play for a short amount of time; therefore,
the MediaPlayer is far more memory efficient for longer clips, especially if they have been compressed,
for instance, using the MP3 (digital audio) or MPEG4 (digital video) file formats or the OGG Vorbis (digital
audio), FLAC (digital audio), or WebM (ON2 VP6, VP8, or VP9) digital video formats.
The AudioClip class has a half-dozen digital audio properties that affect the sound balance, cycles,
location, priority, rate, and volume. These include the balance DoubleProperty, which controls the (relative)
left and right volume levels for the AudioClip object, and the pan DoubleProperty, which controls where
the relative “center” is for the audioClip object. The rate DoubleProperty controls the relative rate (speed) at
which an AudioClip is played, and the volume DoubleProperty controls the relative volume level at which
the AudioClip is played. The cycleCount IntegerProperty controls the number of times the AudioClip will be
played when the play() method is invoked. The priority IntegerProperty controls the relative priority of the
AudioClip object with respect to other AudioClip objects.
There is one static int INDEFINITE data field, which when the cycleCount is set to this value, the AudioClip
will loop continuously until it is stopped using the stop() method call, which we will be learning about soon.

Free download pdf