I'm trying to apply sound today, and the goal in mind was a simple violin tuner. The actionListener seems repetitive/  How might I optimize it (what I tried just broke everything)? I also noticed the sounds bleed into each other.  I don't mind that so much for the different keys (though I'm thinking of a toggle option for this) but it also is the case for the same key, which is undesirable.  I'm not sure how to handle that./* Author: Luigi VincentA simple violin tuner, emits G, D, A, and E sounds to facilitate violin tuning*/import javax.swing.*;import javax.sound.sampled.*;import java.awt.*;import java.awt.event.*;import java.io.*;public class ViolinTuner {    public static void main(String[] args) {        JFrame frame = new JFrame(Violin Tuner);        // get and set icon for the program        ImageIcon icon = new ImageIcon(Images/Icon.png);        frame.setIconImage(icon.getImage());        // Buttons        final JButton G_KEY = new JButton(G);        final JButton D_KEY = new JButton(D);        final JButton A_KEY = new JButton(A);        final JButton E_KEY = new JButton(E);        // Listener to play sounds on click        ActionListener violinSounds = new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                if (e.getSource() == G_KEY) {                    try {                        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(Sounds/G.wav).getAbsoluteFile());                        Clip clip = AudioSystem.getClip();                        clip.open(audioInputStream);                        clip.start();                    }catch(Exception x) { x.printStackTrace(); }                }                else if (e.getSource() == D_KEY) {                    try {                        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(Sounds/D.wav).getAbsoluteFile());                        Clip clip = AudioSystem.getClip();                        clip.open(audioInputStream);                        clip.start();                    }catch(Exception x) { x.printStackTrace(); }                }                else if (e.getSource() == A_KEY) {                    try {                        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(Sounds/A.wav).getAbsoluteFile());                        Clip clip = AudioSystem.getClip();                        clip.open(audioInputStream);                        clip.start();                    }catch(Exception x) { x.printStackTrace(); }                }                else if (e.getSource() == E_KEY) {                    try {                        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(Sounds/E.wav).getAbsoluteFile());                        Clip clip = AudioSystem.getClip();                        clip.open(audioInputStream);                        clip.start();                    } catch(Exception x) { x.printStackTrace(); }                }            }        };        // Register buttons to listener        G_KEY.addActionListener(violinSounds);        D_KEY.addActionListener(violinSounds);        A_KEY.addActionListener(violinSounds);        E_KEY.addActionListener(violinSounds);        // buttons to panel        JPanel p = new JPanel();        p.add(G_KEY);        p.add(D_KEY);        p.add(A_KEY);        p.add(E_KEY);        // Panel to frame        frame.add(p);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setSize(225, 75);        frame.setLocationRelativeTo(null);        frame.setVisible(true);    }};
Using sounds in Java
java;beginner;event handling;audio
Use MIDIWhy play a WAV file, when you could synthesize the tones using MIDI?  The tones can be played with indefinite length.  You wouldn't need to distribute supplementary files with the program.  You can also easily modify the program to generate other pitches without having to obtain new WAV recordings.import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.GridLayout;import javax.sound.midi.*;import javax.swing.*;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;public class ViolinTuner {    private static enum Note {        G(55), D(62), A(69), E(76);        public final int midiNoteNumber;        Note(int midiNoteNumber) {            this.midiNoteNumber = midiNoteNumber;        }    }    // Orchestras tune to oboes.    // http://music.stackexchange.com/q/22133/1941    // http://www.midi.org/techspecs/gm1sound.php    private static final int OBOE = 69;    private final MidiChannel channel;    private final AbstractButton[] buttons = new AbstractButton[Note.values().length];    public ViolinTuner() throws MidiUnavailableException {        this.channel = initMidiChannel(OBOE);        JFrame frame = new JFrame(Violin Tuner);        frame.setIconImage(new ImageIcon(Images/Icon.png).getImage());        // buttons to panel        JPanel notePanel = new JPanel(new GridLayout(2, 0, 8, 4));        notePanel.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));        int i = 0;        for (Note note : Note.values()) {            notePanel.add(buttons[i++] = makeButton(note));        }        // Component to impose a minimum width on the JFrame to ensure that the        // window title is not truncated        JPanel widener = new JPanel();        widener.setPreferredSize(new Dimension(250, 0));        // Panels to frame        frame.setLayout(new BorderLayout());        frame.add(notePanel, BorderLayout.CENTER);        frame.add(widener, BorderLayout.SOUTH);        frame.pack();        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setLocationRelativeTo(null);        frame.setVisible(true);    }    private static MidiChannel initMidiChannel(int instrument) throws MidiUnavailableException {        Synthesizer synth = MidiSystem.getSynthesizer();        synth.open();        MidiChannel channel = synth.getChannels()[0];        // MIDI instruments are traditionally numbered from 1,        // but the javax.midi API numbers them from 0        channel.programChange(instrument - 1);        channel.setChannelPressure(5);  // optional vibrato        return channel;    }    private AbstractButton makeButton(final Note note) {        AbstractButton button = new JToggleButton(note.name());        button.addChangeListener(new ChangeListener() {            @Override            public void stateChanged(ChangeEvent e) {                if (((AbstractButton)e.getSource()).isSelected()) {                    for (AbstractButton b : ViolinTuner.this.buttons) {                        // Turn off any note that might be being played                        if (b != e.getSource()) {                            b.setSelected(false);                        }                    }                    ViolinTuner.this.play(note);                } else {                    ViolinTuner.this.silence();                }            }        });        return button;    }    public void play(Note note) {        this.channel.noteOn(note.midiNoteNumber, 127);     }    public void silence() {        this.channel.allNotesOff();    }    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() {            public void run() {                try {                    new ViolinTuner();                } catch (MidiUnavailableException noMidi) {                    noMidi.printStackTrace();                    System.exit(1);                }            }        });    }}