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.

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

The <strong>Java</strong> Sound API is a simpler way <strong>to</strong> incorporate audio media in<strong>to</strong> applications than<br />

the <strong>Java</strong> Media Framework. The <strong>Java</strong> Sound API is bundled with <strong>Java</strong> 2 Software Development<br />

Kit version 1.3. The API consists of four packages—javax.sound.midi,<br />

javax.sound.midi.spi, javax.sound.sampled and javax.sound.sampled.spi.<br />

The next two sections focus on packages javax.sound.midi and<br />

javax.sound.sampled, which provide classes and interfaces for accessing, manipulating<br />

and playing Musical Instrumental Data Interface (MIDI) and sampled audio. The<br />

packages ending in .spi provide developers with the <strong>to</strong>ols <strong>to</strong> add <strong>Java</strong> Sound support for<br />

additional audio formats that are beyond the scope of this book.<br />

The <strong>Java</strong> Sound API provides access <strong>to</strong> the <strong>Java</strong> Sound Engine which creates digitized<br />

audio and captures media from the supported sound devices discussed in Section 22.3. <strong>Java</strong><br />

Sound requires a sound card <strong>to</strong> play audio. A program using <strong>Java</strong> Sound will throw an exception<br />

if it accesses audio system resources on a computer that does not have a sound card.<br />

22.6 Playing Sampled Audio<br />

This section introduces the features of package javax.sound.sampled for playing<br />

sampled audio file formats, which include Sun Audio (.au), Windows Waveform (.wav)<br />

and Macin<strong>to</strong>sh Audio Interchange File Format (.aiff). The program in Fig. 22.5 and<br />

Fig. 22.6 shows how audio is played using these file formats.<br />

When processing audio data, a line provides the path through which audio flows in a<br />

system. One example of a line is a pair of headphones connected <strong>to</strong> a CD player.<br />

Class ClipPlayer (Fig. 22.5) is an example of how lines can be used. It contains an<br />

object that implements interface Clip, which in turn extends interface DataLine. A<br />

Clip is a line that processes an entire audio file rather than reading continuously from an<br />

audio stream. DataLines enhance Lines by providing additional methods (such as<br />

start and s<strong>to</strong>p) for controlling the flow of data, and Clips enhance DataLines by<br />

providing methods for opening Clips and methods for precise control over playing and<br />

looping the audio.<br />

1 // Fig. 22.5: ClipPlayer.java<br />

2 // Plays sound clip files of type WAV, AU, AIFF<br />

3<br />

4 // <strong>Java</strong> core packages<br />

5 import java.io.*;<br />

6<br />

7 // <strong>Java</strong> extension packages<br />

8 import javax.sound.sampled.*;<br />

9<br />

10 public class ClipPlayer implements LineListener {<br />

11<br />

12 // audio input stream<br />

13 private AudioInputStream soundStream;<br />

14<br />

15 // audio sample clip line<br />

16 private Clip clip;<br />

17<br />

Fig. Fig. 22.5 22.5 ClipPlayer plays an audio file (part 1 of 4).

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

Saved successfully!

Ooh no, something went wrong!