Sound files, especially sampled audio files in the uncompressed PCM
format, are quite large, and, except for the shortest audio clips, it
is not generally a good idea to read the entire file into memory.
Instead, these files are typically played in streaming mode so that
only the portion of the sound that is currently playing must reside in
memory. In exchange for reduced memory consumption, we lose the
ability to easily jump to any position within the sound, of course.
Example 17-4 is a listing of
PlaySoundStream, a program that
plays streaming audio data read from the specified URL. It
demonstrates streaming techniques for both sampled audio and MIDI
data, and also demonstrates a transcoding technique for converting
unsupported encodings (such as ALAW and ULAW) into PCM encoding so
they can be played.
is a console-based application with no GUI. Note the use of the
wait( ) and
notify( ) methods to make
streamMidiSequence( ) into a modal method that does not return until the
sound has finished playing. Note also that you must use a
-m command-line argument in order to play
Example 17-4. PlaySoundStream.java
package je3.sound; import java.io.*; import java.net.*; import javax.sound.sampled.*; import javax.sound.midi.*; /** * This class plays sounds streaming from a URL: it does not have to preload * the entire sound into memory before playing it. It is a command-line * application with no gui. It includes code to convert ...