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
- public AudioData (byte buffer[])
- The
AudioData
constructor accepts a byte arraybuffer
and creates an instance ofAudioData
. The buffer should contain 8000 Hz µLaw audio data.
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
- public AudioStream (InputStream in) throws IOException
- The
AudioStream
constructor hasInputStream
in
as its parameter and can throwIOException
on error. In the following code, we get an input stream by opening a .au file. Another common way to construct anAudioStream
is to use the stream associated with a URL through the URL'sopenStream()
method.FileInputStream fis = new FileInputStream ("/usr/openwin/demo/sounds/1.au"); AudioStream audiostream = new AudioStream (fis);
or:
AudioStream audiostream = new AudioStream (savedUrl.openStream());
If you are constructing the audio data yourself, you would use a
ByteArrayInputStream
. Whatever the source of the data, the input stream should provide data in Sun's .au format.
- public int read (byte buffer[], int offset, int length) throws IOException
- The
read()
method forAudioStream
reads an array of bytes intobuffer
.offset
is the first element ofbuffer
that is used.length
is the maximum number of bytes to read. This method blocks until some input is available.read()
returns the actual number of bytes read. If the end of stream is encountered and no bytes were read,read()
returns -1. Ordinarily, youread()
anAudioStream
only if you want to modify the audio data in some way. - public int getLength()
- The
getLength()
method returns the length of the audio data contained within theAudioStream
, excluding any header information in the file. - public AudioData getData () throws IOException
- The
getData()
method ofAudioStream
is the most important and most frequently used. It reads the data from the input stream and creates anAudioData
instance. As the following code shows, you can create anAudioStream
and get theAudioData
with one statement.
AudioData audiodata = new AudioStream (aUrl.openStream()).getData();
AudioDataStream
Constructors- public AudioDataStream (AudioData data)
- This constructor creates an
AudioDataStream
from anAudioData
objectdata
. The resultingAudioDataStream
is a subclass ofByteArrayInputStream
and can be played by theAudioPlayer.start()
method.
There are no methods for AudioDataStream
.
ContinuousAudioDataStream
Constructors- public ContinuousAudioDataStream (AudioData data)
- This constructor creates a continuous stream of audio from
data
. The resultingContinuousAudioDataStream
is a subclass ofAudioDataStream
and, therefore, ofByteArrayInputStream
. It can be played byAudioPlayer.start()
; whenever the player reaches the end of the continuous audio data stream, it restarts from the beginning.
- public int read ()
- This
read()
method ofContinuousAudioDataStream
overrides theread()
method inByteArrayInputStream
to rewind back to the beginning of the stream when end-of-file is reached. This method is used by the system when it reads theInputStream
; it is rarely called directly.read()
never returns -1 since it loops back to the beginning on end-of-file. - public int read (byte buffer[], int offset, int length)
- This
read()
method ofContinuousAudioDataStream
overrides theread()
method inByteArrayInputStream
to rewind back to the beginning of the stream when end-of-file is reached. This method is used by the system when it reads theInputStream
; it is rarely called directly.read()
returns the actual number of bytes read.read()
never returns -1 since it loops back to the beginning on end-of-file.
AudioStreamSequence
Constructors- public AudioStreamSequence (Enumeration e)
- The constructor for
AudioStreamSequence
accepts anEnumeration
e
(normally the elements of aVector
ofAudioStream
s) as its sole parameter. The constructor converts the sequence of audio streams into a single stream to be played in order. An example follows:
Vector v = new Vector (); v.addElement (new AudioStream (url1.openStream ()); v.addElement (new AudioStream (url2.openStream ()); AudioStreamSequence audiostream = new AudioStreamSequence (v.elements ());Methods
- public int read ()
- This
read()
method ofAudioStreamSequence
overrides theread()
method inInputStream
to start the next stream when end-of-file is reached. This method is used by the system when it reads theInputStream
and is rarely called directly. If the end of all streams is encountered and no bytes were read,read()
returns -1. Otherwise,read()
returns the character read. - public int read (byte buffer[], int offset, int length)
- This
read()
method ofAudioStreamSequence
overrides theread()
method inInputStream
to start the next stream when end-of-file is reached. This method is used by the system when it reads theInputStream
and is rarely called directly.read()
returns the actual number of bytes read. If the end of all streams is encountered and no bytes were read,read()
returns -1.
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
- public final static AudioPlayer player
player
is the default audio player. This audio player is initialized automatically when the class is loaded; you do not have to initialize it (in fact, you can't because it is final) or call the constructor yourself.
- public synchronized void start (InputStream in)
- The
start()
method starts a thread that plays theInputStream
in
. Streamin
continues to play until there is no more data or it is stopped. Ifin
is aContinuousAudioDataStream
, the playing continues untilstop()
(described next) is called. - public synchronized void stop (InputStream in)
- The
stop()
method stops the player from playingInputStream
in
. Nothing happens if the streamin
is no longer playing or was never started.
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(); } }