O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 * range;
float newValue = rangeOffset + min;
control.setValue (newValue);
}
// ChangeListener implementation
public void stateChanged (ChangeEvent e) {
setControlFromSlider( );
}
}
Example 10-14. Swing widget for a JavaSound FloatControl (continued)
406
|
Chapter 10, Audio
#78 Provide Audio Controls During Playback
HACK
Example 10-15. Creating an audio player with GUI controls
import javax.sound.sampled.*;
public class DataLineControlGUI extends JPanel {
PCMFilePlayer player;
JButton startButton;
public DataLineControlGUI (File f) {
super( );
try {
player = new PCMFilePlayer (f);
} catch (Exception ioe) {
add (new JLabel ("Error: " +
ioe.getMessage( )));
return;
}
DataLine line = player.getLine( );
// layout
// line 0: name
setLayout (new GridBagLayout( ));
GridBagConstraints gbc = new GridBagConstraints( );
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.SOUTH;
add (new JLabel ("File: " +
player.getFile().getName( )), gbc);
// subsequent lines: controls
gbc.gridwidth = 1;
Control[] controls = line.getControls( );
for (int i=0; i<controls.length; i++) {
gbc.gridx = 0;
gbc.gridy++;
gbc.anchor = GridBagConstraints.EAST;
add (new JLabel(controls[i].getType().toString( )), gbc);
JComponent controlComp =
ControlComponentFactory.getComponentFor (controls[i]);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.WEST;
add (controlComp, gbc);
}
// now start playing
player.start( );
}
Provide Audio Controls During Playback #78
Chapter 10, Audio
|
407
HACK
When run with a suitable audio file, the resulting GUI looks like
Figure 10-11. Note that it’s possible you might have other control widgets
(or an unsupported label for
EnumControls and CompoundControls) if Java-
Sound gives you different controls than it did when I wrote and ran this on
Java 1.4.2.
public static void main (String[] args) {
JFileChooser chooser = new JFileChooser( );
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile( );
DataLineControlGUI demo =
new DataLineControlGUI (file);
JFrame f = new JFrame ("JavaSound control");
f.getContentPane( ).add (demo);
f.pack( );
f.setVisible(true);
}
}
Figure 10-11. JavaSound audio player with GUI controls
Example 10-15. Creating an audio player with GUI controls (continued)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required