import javax.sound.sampled.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class AudioSample extends JPanel implements ActionListener { private String[] names = { "Audio/cat1.wav", "Audio/dog1.wav", "Audio/bird.wav" }; private Clip[] clips; private int i = -1; public AudioSample() { JButton b = new JButton("Play!"); b.addActionListener(this); setLayout(new FlowLayout()); add(b); try { clips = new Clip[names.length]; for (int j = 0; j < names.length; j++) { Clip c = AudioSystem.getClip(); // c.open(AudioSystem.getAudioInputStream(new File(names[j]))); URL u = getClass().getResource(names[j]); c.open(AudioSystem.getAudioInputStream(u)); clips[j] = c; } } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) { ex.printStackTrace(); } } public void actionPerformed(ActionEvent e) { if (i >= 0) { clips[i].stop(); } i = (i + 1) % names.length; clips[i].loop(Clip.LOOP_CONTINUOUSLY); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Audio Sample"); frame.add(new AudioSample()); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }); } }