Audio in Applications

The rest of this chapter describes how to use audio in your applications. Because the audio support discussed so far has been provided by the browser, applications that don't run in the context of a browser must use a different set of classes to work with audio. These classes are within the sun.audio package. Although the sun.* package hierarchy is not necessarily included by other vendors, the sun.audio classes discussed here are provided with Netscape Navigator 2.0/3.0 and Internet Explorer 3.0. Therefore, you can use these classes within applets, too. This section ends by developing a SunAudioClip class that has an interface similar to the applet's audio interface; you can use it to minimize coding differences between applets and applications.

AudioData

The AudioData class holds a clip of 8000 Hz µLaw audio data. This data can be used to construct an AudioDataStream or ContinuousAudioDataStream, which can then be played with the AudioPlayer. Constructor

Methods

There are no methods for AudioData.

AudioStream

AudioStream subclasses FilterInputStream, which extends InputStream. Using an InputStream lets you move back and forth (rewind and fast forward) within an audio file, in addition to playing the audio data from start to finish. Constructors

Methods
AudioData audiodata = new AudioStream (aUrl.openStream()).getData(); 

AudioDataStream

Constructors Methods

There are no methods for AudioDataStream.

ContinuousAudioDataStream

Constructors Methods

AudioStreamSequence

Constructors
Vector v = new Vector (); v.addElement (new AudioStream (url1.openStream ()); v.addElement (new AudioStream (url2.openStream ()); AudioStreamSequence audiostream = new AudioStreamSequence (v.elements ()); 
Methods

AudioPlayer

The AudioPlayer class is the workhorse of the sun.audio package. It is used to play all the streams that were created with the other classes. There is no constructor for AudioPlayer; it just extends Thread and provides start() and stop() methods. Variable

Methods

SunAudioClip Class Definition

The class in Example 14.3 is all you need to play audio files in applications. It implements the java.applet.AudioClip interface, so the methods and functionality will be familiar. The test program in main() demonstrates how to use the class. Although the class itself can be used in applets, provided your users have the sun.audio package available, it is geared towards application users.

Example 14.3: The SunAudioClip Class

import java.net.URL; import java.io.FileInputStream; import sun.audio.*;
public class SunAudioClip implements java.applet.AudioClip {
 private AudioData audiodata; private AudioDataStream audiostream; private ContinuousAudioDataStream continuousaudiostream; static int length;
public SunAudioClip (URL url) throws java.io.IOException {
 audiodata = new AudioStream (url.openStream()).getData(); audiostream = null; continuousaudiostream = null;
}
public SunAudioClip (String filename) throws java.io.IOException {
 FileInputStream fis = new FileInputStream (filename); AudioStream audioStream = new AudioStream (fis); audiodata = audioStream.getData(); audiostream = null; continuousaudiostream = null;
}
public void play () {
 audiostream = new AudioDataStream (audiodata); AudioPlayer.player.start (audiostream);
}
public void loop () {
 continuousaudiostream = new ContinuousAudioDataStream (audiodata); AudioPlayer.player.start (continuousaudiostream);
}
public void stop () {
 if (audiostream != null) AudioPlayer.player.stop (audiostream); if (continuousaudiostream != null) AudioPlayer.player.stop (continuousaudiostream);
}
public static void main (String args[]) throws Exception {
 URL url1 = new URL ("http://localhost:8080/audio/1.au"); URL url2 = new URL ("http://localhost:8080/audio/2.au"); SunAudioClip sac1 = new SunAudioClip (url1); SunAudioClip sac2 = new SunAudioClip (url2); SunAudioClip sac3 = new SunAudioClip ("1.au"); sac1.play (); sac2.loop (); sac3.play (); try {// Delay for loop Thread.sleep (2000);
}
catch (InterruptedException ie) {} sac2.stop();
}
}