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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

122 // if replay set, replay forever<br />

123 else<br />

124<br />

125 if ( lineEvent.getType() == LineEvent.Type.STOP &&<br />

126 replay ) {<br />

127<br />

128 System.out.println( "replay" );<br />

129<br />

130 // replay clip forever<br />

131 clip.loop( Clip.LOOP_CONTINUOUSLY );<br />

132 }<br />

133 }<br />

134<br />

135 // set replay of clip<br />

136 public void setReplay( boolean value )<br />

137 {<br />

138 replay = value;<br />

139 }<br />

140<br />

141 // s<strong>to</strong>p and close clip, returning system resources<br />

142 public void close()<br />

143 {<br />

144 if ( clip != null ) {<br />

145 clip.s<strong>to</strong>p();<br />

146 clip.close();<br />

147 }<br />

148 }<br />

149<br />

150 } // end class ClipPlayer<br />

Fig. Fig. 22.5 22.5 ClipPlayer plays an audio file (part 4 of 4).<br />

All Lines generate LineEvents, which can be handled by LineListeners.<br />

LineEvents occur when starting, s<strong>to</strong>pping, playing and closing a Line object. Although<br />

a Line s<strong>to</strong>ps playback au<strong>to</strong>matically when it reaches the end of an audio file, class Clip-<br />

Player implements interface LineListener (line 10) and can close the Clip permanently<br />

or replay the Clip (discussed shortly). LineListeners are useful for tasks that<br />

must be synchronized with the LineEvent states of a line.<br />

The Clip reads audio data from an AudioInputStream (a subclass of Input-<br />

Stream), which provides access <strong>to</strong> the stream’s data content. This example loads clips of<br />

the audio data before attempting <strong>to</strong> play it, and therefore is able <strong>to</strong> determine the length of<br />

the clip in frames. Each frame represents data at a specific time interval in the audio file.<br />

To play sampled audio files using <strong>Java</strong> Sound, a program must obtain an AudioInput-<br />

Stream from an audio file, obtain a formatted Clip line, load the AudioInput-<br />

Stream in<strong>to</strong> the Clip line and start the data flow in the Clip line.<br />

To play back sampled audio, the audio stream must be obtained from an audio file.<br />

ClipPlayer method openFile (lines 31–54) obtains audio from soundFile (initialized<br />

in the ClipPlayer construc<strong>to</strong>r at lines 25–28). Lines 35–36 call AudioSystem<br />

static method getAudioInputStream <strong>to</strong> obtain an AudioInputStream for<br />

soundFile. Class AudioSystem facilitates access <strong>to</strong> many of the resources needed <strong>to</strong><br />

play and manipulate sound files. Method getAudioInputStream throws an Unsup-

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

Saved successfully!

Ooh no, something went wrong!