I have this code which plays wav files from a folder with play and stop. How can I add pause and continue?
import java.awt.*;
import java.awt.event.*;
imp
I think I have an easier solution that does not use internal classes. You can create a clip by only a line code. I used JAVA 1.6 on order that more people can use the code.
Here are my classes:
MainClass :
import java.awt.EventQueue;
public class MainClass {
public static void main(String[] _args) {
EventQueue.invokeLater(new LaunchingWindow());
}
}
LaunchingWindow :
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class LaunchingWindow extends Thread {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception _0) {
}
JFrame frame_ = new JFrame("Testing");
frame_.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame_.setLayout(new BorderLayout());
frame_.add(new MusicContainer());
frame_.pack();
frame_.setLocationRelativeTo(null);
frame_.setVisible(true);
}
}
MusicContainer:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MusicContainer extends JPanel {
private Clip clip;
private JTextField audioFile;
private JButton play;
private JButton stop;
private int lastFrame;
public MusicContainer() {
setLayout(new GridBagLayout());
audioFile = new JTextField(20);
play = new JButton(">");
stop = new JButton("[]");
JPanel controls_ = new JPanel();
controls_.add(play);
controls_.add(stop);
GridBagConstraints gbc_ = new GridBagConstraints();
gbc_.gridx = 0;
gbc_.gridy = 0;
gbc_.weightx = 1;
gbc_.fill = GridBagConstraints.HORIZONTAL;
add(audioFile, gbc_);
gbc_.gridy++;
add(controls_, gbc_);
play.addActionListener(new PlayingMusic(this));
stop.addActionListener(new StoppingMusic(this));
}
public void playMusic() {
if (clip == null) {
try {
loadClip(new File(audioFile.getText()));
clip.start();
clip.addLineListener(new MusicListener(play));
play.setText("||");
} catch (Exception _0) {
_0.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to load audio clip", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
if (clip.isRunning()) {
lastFrame = clip.getFramePosition();
clip.stop();
} else {
if (lastFrame < clip.getFrameLength()) {
clip.setFramePosition(lastFrame);
} else {
clip.setFramePosition(0);
}
clip.start();
}
}
}
public void stopMusic() {
if (clip != null) {
lastFrame = 0;
clip.stop();
}
}
protected void loadClip(File _audioFile) throws Exception {
AudioInputStream audioStream_ = AudioSystem.getAudioInputStream(_audioFile);
//AudioSystem.getClip() is much easier
clip = AudioSystem.getClip();
clip.open(audioStream_);
}
}
MusicListener :
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.swing.JButton;
public class MusicListener implements LineListener {
private static final String START = "START";
private static final String OPEN = "OPEN";
private static final String STOP = "STOP";
private static final String CLOSE = "CLOSE";
private JButton play;
public MusicListener(JButton _play) {
play = _play;
}
@Override
public void update(LineEvent _event) {
String eventType_ = _event.getType().toString();
if (eventType_.equalsIgnoreCase(START)) {
play.setText("||");
} else if (eventType_.equalsIgnoreCase(OPEN)) {
System.out.println("Open");
} else if (eventType_.equalsIgnoreCase(STOP)) {
System.out.println("close");
play.setText(">");
} else if (eventType_.equalsIgnoreCase(CLOSE)) {
play.setText(">");
}
}
}
PlayingMusic :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PlayingMusic implements ActionListener {
private MusicContainer container;
public PlayingMusic(MusicContainer _container) {
container = _container;
}
@Override
public void actionPerformed(ActionEvent _e) {
container.playMusic();
}
}
StoppingMusic :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StoppingMusic implements ActionListener {
private MusicContainer container;
public StoppingMusic(MusicContainer _container) {
container = _container;
}
@Override
public void actionPerformed(ActionEvent _e) {
container.stopMusic();
}
}