26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1282 <strong>Java</strong> Media Framework and <strong>Java</strong> Sound (on CD) Chapter 22<br />

portedAudioFileException if the specified sound file is a non-audio file or if it<br />

contains a format that is not supported by <strong>Java</strong> Sound.<br />

Next the program must provide a line through which audio data can be processed. Line<br />

52 invokes method loadClip (lines 57–106) <strong>to</strong> open a Clip line and load the audio<br />

stream for playback. Line 81 invokes AudioSystem static method getLine <strong>to</strong><br />

obtain a Clip line for audio playback. Method getLine requires a Line.Info object<br />

as an argument, <strong>to</strong> specify the attributes of the line that the AudioSystem should return.<br />

The line must be able <strong>to</strong> process audio clips of all supported sampled audio formats, so the<br />

DataLine.Info object must specify a Clip data line and a general encoding format. A<br />

buffer range should also be specified so the program can determine the best buffer size. The<br />

DataLine.Info construc<strong>to</strong>r receives four arguments. The first two are the format (of<br />

type AudioFormat.Encoding) in<strong>to</strong> which the program should convert the audio data<br />

and the AudioFormat of the audio source. The AudioFormat sets the format supported<br />

by the line, according <strong>to</strong> the audio format of the stream. Line 63 obtains the Audio-<br />

Format of the AudioInputStream, which contains format specifications that the<br />

underlying system uses <strong>to</strong> translate the data in<strong>to</strong> sounds. Lines 68–69 call AudioSystem<br />

method getTargetFormats <strong>to</strong> obtain an array of the supported AudioFormats. The<br />

third argument of the DataLine.Info construc<strong>to</strong>r, which specifies the minimum buffer<br />

size, is set <strong>to</strong> the number of bytes in each frame of the audio stream. Line 70 invokes<br />

AudioFormat method getFrameSize <strong>to</strong> obtain the size of each frame in the audio<br />

stream. The maximum buffer size should be equivalent <strong>to</strong> two frames of the audio stream<br />

(line 71). Using the DataLine.Info object, line 74 checks if the underlying audio<br />

system supports the specified line. If it does, line 81 obtains the line from the audio system.<br />

When an audio clip starts playing and when it finishes, the program needs <strong>to</strong> be alerted.<br />

Line 84 registers a LineListener for the Clip’s LineEvents. If a LineEvent<br />

occurs, the program calls LineListener method update (lines 115–133) <strong>to</strong> process it.<br />

The four LineEvent types, as defined in class LineEvent.Type, are OPEN, CLOSE,<br />

START and STOP. When the event type is LineEvent.Type.STOP and variable<br />

replay is false, line 120 calls ClipPlayer’s close method (lines 142–148) <strong>to</strong> s<strong>to</strong>p<br />

audio playback and close the Clip. All audio resources obtained previously by the Clip<br />

are released when audio playback s<strong>to</strong>ps. When the event type is<br />

LineEvent.Type.STOP and variable replay is true, line 131 calls Clip method<br />

loop with parameter Clip.LOOP_CONTINUOUSLY, causing the Clip <strong>to</strong> loop until the<br />

user terminates the application. Invoking method s<strong>to</strong>p of interface Clip s<strong>to</strong>ps data<br />

activity in the Line. Invoking method start resumes data activity.<br />

Once the program finishes validating the Clip, line 87 calls Clip method open with<br />

the AudioInputStream soundStream as an argument. The Clip obtains the system<br />

resources required for audio playback. AudioSystem method getLine and Clip<br />

method open throw LineUnavailableExceptions if another application is using<br />

the requested audio resource. Clip method open also throws an IOException if the<br />

Clip cannot read the specified AudioInputStream. When the test program (Fig. 22.6)<br />

calls ClipPlayer method play (lines 109–112), the Clip method start begins<br />

audio playback.<br />

Class ClipPlayerTest (Fig. 22.6) enables users <strong>to</strong> specify an audio file <strong>to</strong> play by<br />

clicking the Open Audio Clip but<strong>to</strong>n. When users click the but<strong>to</strong>n, method action-<br />

Performed (lines 37–58) prompts an audio file name and location (line 39) and creates

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!