Game Development Reference
In-Depth Information
java.lang.Object
> javafx.scene.media. AudioClip
Your AudioClip objects will each reference one digital audio sample in memory.
These can be triggered with virtually zero latency, which is what makes this class the
perfect class to use for Java 8 games development. AudioClip objects are loaded simil-
arly to Media (long-form audio or video) objects, using URL objects, but have a vastly
different behavior. For example, a Media object cannot play itself; it will need a Medi-
aPlayer object, and also the MediaView object, if it contains digital video. Media ob-
jects would be better suited for long-form digital audio assets (like music), which can-
not fit in memory all at the same time, and must be streamed for optimal memory util-
ization. A MediaPlayer will only have enough decompressed digital audio data “pre-
rolled” into memory to play for a short amount of time, so a MediaPlayer approach is
much more memory efficient for long digital audio clips, especially if they are com-
pressed.
The AudioClip object is usable immediately upon instantiation, as you'll see later
on during this chapter, and this is an important attribute to have when it comes to Java
8 game development. AudioClip object playback behavior can be said to be “fire and
forget,” which means that once one of your playiSound() method calls is invoked, your
only operable control at that point over the digital audio sample is to call the AudioClip
.stop() method.
Interestingly, your AudioClip objects may also be triggered (played) multiple
times, and AudioClips can even be triggered simultaneously, as you will see a bit later
on during this chapter. To accomplish this same result by using a Media object, you
would have to create a new MediaPlayer object for each sound that you want to play in
parallel.
The reason that the AudioClip object is so versatile (responsive) is because your
AudioClip objects are stored in memory. The AudioClip object uses a raw, uncom-
pressed digital audio sample representing the entire sound. This is placed into memory
in its raw, uncompressed state, which is why in the next section of the chapter we're
going to use the WAVE audio file format. This audio format applies zero compression,
and thus, the resulting file size for optimized digital audio samples will also represent
the amount of system memory that each of these samples will utilize.
What is really impressive about the AudioClip class, however, is the amount of
power it gives developers via its properties and the three overloaded .play() method
calls. You can set up sample playback priorities, shift the pitch (frequency of the
sound) up to 8 times (higher octaves) or down to 1/8th (lower octaves), pan the sound
anywhere in the spatial spectrum, control the left and right balance of the sound, con-
Search WWH ::




Custom Search