Storing Clip Information

A ClipInfo object is responsible for loading a clip and plays, pauses, resumes, stops, and loops that clip when requested by ClipsLoader. Additionally, an object implementing the SoundsWatcher interface (a watcher) can be notified when the clip loops or stops.

Much of the manipulation carried out by ClipInfo, such as clip loading, is almost identical to that found in PlayClip.java in Chapter 7. Perhaps the largest difference is that PlayClip exits when it encounters a problem, and ClipInfo prints an error message and soldiers on.

loadClip() is similar to PlayClip's loadClip(), so certain parts have been commented away in the code below to simplify matters:

 // global private Clip clip = null; private void loadClip(String fnm) { try { // 1. access the audio file as a stream AudioInputStream stream = AudioSystem.getAudioInputStream( getClass().getResource(fnm) ); // 2. Get the audio format for the data in the stream AudioFormat format = stream.getFormat(); // convert ULAW/ALAW formats to PCM format... // several lines, which update stream and format // 3. Gather information for line creation DataLine.Info info = new DataLine.Info(Clip.class, format); // make sure the sound system supports the data line if (!AudioSystem.isLineSupported(info)) { System.out.println("Unsupported Clip File: " + fnm); return; } // 4. create an empty clip using the line information clip = (Clip) AudioSystem.getLine(info); // 5. Start monitoring the clip's line events clip.addLineListener(this); ...

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.