Provide Audio Controls During Playback #78
Chapter 10, Audio
|
401
HACK
Of course, Sun could just go and actually implement getLevel( )…wouldn’t
that be nice?
H A C K
#78
Provide Audio Controls During Playback Hack #78
Let your users take control of JavaSound playback.
To complete this set of JavaSound-related hacks, why not give the user the
opportunity to control the sound as it plays? JavaSound provides a very
dynamic means of getting at controls like gain and pan (more commonly
thought of as volume and balance) through a discovery mechanism that you
can use to support any kind of control that might exist, even a control you
know nothing about.
On the other hand, JavaSound presents a control not as a GUI widget, but
just as an object that can affect the behavior of a
Line. This hack will help
you provide the GUI side.
The
Control class simply defines a getType( ) and toString( ) method.
What’s more interesting is its subclasses, each of which defines a different
kind of control:
BooleanControl
Controls a value that can be either true or false
EnumControl
Controls a value that can be one of n known values
FloatControl
Controls a value that is expressed as a floating-point number
CompoundControl
Controls multiple properties, and itself contains multiple controls
You can get the
Controls supported by your Line simply by calling Line.
getControls( )
, which returns an array of Controls. You can also ask for a
specific control by using a constant of the
Control.Type subclass, such as
BooleanControl.Type.MUTE or FloatControl.Type.MASTER_GAIN. Pass this con-
stant to
Line.isControlSupported( ) to see if the control is available for the
given line, and then get the control object with
Line.getControl( ).
If you look at the subclasses of
Control, you’ll see that each provides getter
and setter methods appropriate to its data type.
BooleanControl, for exam-
ple, has a
getValue( ) that returns a boolean and a setValue( ) that takes a
boolean. FloatControl has similar methods that work with floats. Each also
provides a number of what might be called “verbiage methods” that provide
text for building a GUI. For example,
FloatControl offers label names for its
minimum, maximum, and middle values, and another that describes the

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.