The Sound Player
SoundPlayer.java (located in SoundExamps/SoundPlayer/) shows off the capabilities of the AudioClip
class (see Figure 7-1) in a longer example.
Figure 7-1. The SoundPlayer application
A selection of sound files in different formats (all located in the Sounds/ subdirectory below SoundPlayer/) are offered up. They can be played once, looped, or stopped. It's possible to have multiple clips playing and looping simultaneously, and the stop button terminates all the currently playing clips. This example is somewhat similar to the Java Sound tutorial example, SoundApplication
.
Figure 7-2 gives the class diagram for SoundPlayer
, showing all the public and private methods and variables in the class.
Two important data structures are in play here:
private HashMap soundsMap; private ArrayList playingClips;
soundsMap
holds the loaded AudioClip
s, indexed by their filenames. playingClips
maintains a list of currently playing AudioClip
s (or, to be more precise, what I think is playing).
Figure 7-2. Class diagram for SoundPlayer
loadSounds()
loads the AudioClip
s and stores them in soundsMap
for later use:
private void loadSounds()
{
soundsMap = new HashMap();
for (int i=0; i < soundFNms.length; i++) {
AudioClip clip = Applet.newAudioClip( getClass().getResource(SOUND_DIR + soundFNms[i]) ...
Get Killer Game Programming in Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.