
Provide Audio Controls During Playback #78
Chapter 10, Audio
|
405
HACK
Having provided these two implementations, it should be clear how you
would create an
EnumControlComponent. The EnumControl.getValues( ) method
returns a
String array that you would use as the model of an uneditable
JComboBox. You’d use getValue( ) to set one of these as the initial value, and
then on user events, you could pull out the
JComboBox’s selection and set the
EnumControl with setValue( ).
Check It Out!
The DataLineControlGUI shown in Example 10-15 is a simple JPanel that
contains a
JLabel with the name of the file to be played on the first line of a
GridBagLayout, and then control-name JLabels and factory-generated con-
trol widgets on each successive line. The sound is played by the
PCMFilePlayer, which was introduced as part of playing uncompressed
AIFFs and WAVs of arbitrary lengths
[Hack #76].
private void setSliderFromControl( ) {
// figure out value as percent of range
float offsetValue = control.getValue( ) - min;
float percent = 0.0f;
if (range != 0.0)
percent = offsetValue / range;
// apply that to SLIDER_RANGE
int sliderValue = (int) (percent * SLIDER_RANGE);
slider.setValue (sliderValue);
}
private void setControlFromSlider( ) {
// figure out slider percentage
float sliderPercentage =
(float) slider.getValue( ) / SLIDER_RANGE;
// figure out value for that percentage of range
float rangeOffset = sliderPercentage ...