Java Reference
In-Depth Information
Controlling the Playback Parameters of an AudioClip
We now build on the preceding example to show how AudioClip allows a sound effect to be played at differing
volumes and rates. We also show how the balance of an audio clip can be adjusted to control the volume of the left
and right channels. This can be useful when you want to achieve a bit of 2D spatialization in your sound effects.
The application we end up with is shown in Figure 9-2 .
Figure 9-2. The CodeMonkeyToDo application
We start by examining the Application class and looking at how the sounds are loaded. Listing 9-3 shows the
application with the scene construction code removed. This allows you to see more easily that we are loading MP3
files from the jar file using the getResource method again. In this case, our sound effects are in a different location
relative to our application class. Therefore we use a path such as resources/coffee.mp3 to locate them. These clips
are a little longer, so the decision was made to use MP3 files to keep the size of the jar file to a minimum at the expense
of a little extra decoding time during startup.
The audio clips are all loaded in the constructor of the CodeMonkeyToDo class. Loading them in the constructor
guarantees that they are loaded at the very start of the program before the start method is called. Alternatively, we
could have overridden the init method of the Application class and loaded our clips there. Notice that the three
AudioClip s and the three Slider controls are declared as fields in the Application class. This allows convenient
access later when the AudioClip is played using the current values from the Slider controls shown in Figure 9-2 .
Listing 9-3. The Application Class of the CodeMonkeyToDo Example Program
public class CodeMonkeyToDo extends Application {
private final AudioClip coffeeClip;
private final AudioClip jobClip;
private final AudioClip meetingClip;
private Slider volumeSlider;
private Slider rateSlider;
private Slider balanceSlider;
 
Search WWH ::




Custom Search