Chapter 8. Loading and Playing Sounds

Chapter 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.

The LoadersTests application
Figure 8-1. The LoadersTests application

The 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.


The LoadersTests application ...

Get Killer Game Programming in Java now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.