Chapter 8. Loading and Playing SoundsChapter 7 introduced the Java Sound API, with small examples showing the playback of clips, streamed audio, and MIDI sequences. This chapter is given over to a single application, LoadersTests, which demonstrates my ClipsLoader and MidisLoader classes for loading, playing, pausing, resuming, stopping, and looping clips and sequences. These loader classes will be used in later chapters for games requiring sounds or music. Figure 8-1 shows the LoadersTests GUI. Figure 8-1. The LoadersTests applicationThe left side of the control panel offers a choice between four MIDI sequences (all with a farming theme). The selection can be played once or repeatedly. Once playing, the Pause and Stop buttons are enabled. If the Pause button is pressed, the music pauses until resumed with the Resume button (which is the Pause button renamed). Only a single sequence can be played at a time. The right side of the control panel is a series of check boxes for turning looping on and off for the "dog," "cat," "sheep," and "chicken" clips. A clip is started by the user clicking on the relevant image in the top half of the GUI. Multiple clips can be played at once, complementing an already playing MIDI sequence. My personal favorite is a looping "Old McDonald" with all the clips playing repeatedly. The joys of silence soon become apparent.
Figure 8-2 shows the class diagrams for LoadersTests, with only the public methods visible. Figure 8-2. Class diagrams for LoadersTestsLoadersTests creates the GUI, initializes the loaders, and deals with user input. The images panel is coded in the SoundsPanel class. The largest class is ImagesLoaders, previously described in Chapter 6. It's used here to load the four animal GIFs, arguably an example of coding overkill for such simple tasks. MidisLoader loads and manages multiple MIDI sequences, with each sequence stored in its own MidiInfo object. ClipsLoader does the same for clips, which are stored in ClipInfo objects. The MIDI sequence and clips can be configured to call atSequenceEnd( ) in SoundsWatcher when they finish playing. In this example, LoadersTests implements the SoundsWatcher interface. |