So, based on this example
I wrapped the pause/resume functionality it separate methods.
You will need to paste the path to an audio file in the JTextField
and press load, then you can press the "Click me to play" label to start playing and "Click me to pause" to pause it.
(Before anybody jumps all over me for not using buttons, the OP is using a MouseListener
, so I'm "trying" to mimic what little of their work flow I can see)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class SimplyPlayer {
public static void main(String[] args) {
new SimplyPlayer();
}
public SimplyPlayer() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Clip clip;
private JTextField audioFile;
private int lastFrame;
public TestPane() {
setLayout(new BorderLayout());
ActionListener loadAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (clip != null) {
clip.stop();
clip.close();
}
try {
loadClip(new File(audioFile.getText()));
System.out.println("loaded");
} catch (LineUnavailableException | IOException | UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
}
};
audioFile = new JTextField(20);
audioFile.addActionListener(loadAction);
JButton load = new JButton("Load");
load.addActionListener(loadAction);
JPanel loadPanel = new JPanel(new BorderLayout());
loadPanel.add(audioFile);
loadPanel.add(load, BorderLayout.EAST);
add(loadPanel, BorderLayout.NORTH);
JPanel panel = new JPanel(new GridBagLayout());
JLabel play = new JLabel("Click me to play");
JLabel pause = new JLabel("Click me to pause");
play.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(50, 50, 50, 50)));
pause.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(50, 50, 50, 50)));
panel.add(play);
panel.add(pause);
add(panel);
MouseListener handler = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getSource() == play) {
resume();
} else if (e.getSource() == pause) {
pause();
}
}
};
play.addMouseListener(handler);
pause.addMouseListener(handler);
}
protected void loadClip(File audioFile) throws LineUnavailableException, IOException, UnsupportedAudioFileException {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
this.clip = (Clip) AudioSystem.getLine(info);
this.clip.open(audioStream);
}
public void pause() {
if (clip != null && clip.isRunning()) {
lastFrame = clip.getFramePosition();
System.out.println("Stop");
clip.stop();
}
}
public void resume() {
if (clip != null && !clip.isRunning()) {
// Make sure we haven't passed the end of the file...
if (lastFrame < clip.getFrameLength()) {
clip.setFramePosition(lastFrame);
} else {
clip.setFramePosition(0);
}
System.out.println("Start");
clip.start();
}
}
}
}
Loading a audio file within an applet is a little more complicated, but is demonstrated here