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.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.AudioClip;
import java.net.URL;
public class JukeBox extends JFrame {
private JComboBox musicCombo;
private JButton stopButton, playButton;
private AudioClip[] music;
private AudioClip current;
public JukeBox(String title) {
super(title);
getContentPane().add(new JukeBoxControls());
}
public static void main(String [] args) {
JukeBox myf = new JukeBox ("WAV COMBOBOX");
myf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myf.pack();
myf.setVisible(true);
}
private class JukeBoxControls extends JPanel {
public JukeBoxControls() {
URL url1, url2, url3, url4, url5;
url1 = url2 = url3 = url4 = url5 = null;
try
{
url1 = new URL ("file", "localhost", "beep-01a.wav");
url2 = new URL ("file", "localhost", "beep-02.wav");
url3 = new URL ("file", "localhost", "beep-03.wav");
url4 = new URL ("file", "localhost", "beep-04.wav");
url5 = new URL ("file", "localhost", "beep-05.wav");
}
catch (Exception exception) {}
music = new AudioClip[7];
music[0] = null;
music[1] = JApplet.newAudioClip (url1);
music[2] = JApplet.newAudioClip (url2);
music[3] = JApplet.newAudioClip (url3);
music[4] = JApplet.newAudioClip (url4);
music[5] = JApplet.newAudioClip (url5);
JLabel titleLabel = new JLabel ("WAV COMBOBOX");
titleLabel.setAlignmentX (Component.CENTER_ALIGNMENT);
String[] musicNames = {"vyberte...", "sound 1",
"sound 2", "sound 3", "sound 4",
"sound 5"};
musicCombo = new JComboBox (musicNames);
musicCombo.setAlignmentX (Component.CENTER_ALIGNMENT);
playButton = new JButton ("Play", new ImageIcon ("play.gif"));
playButton.setBackground (Color.white);
stopButton = new JButton ("Stop", new ImageIcon ("stop.png"));
stopButton.setBackground (Color.white);
JPanel buttons = new JPanel();
buttons.setLayout (new BoxLayout (buttons, BoxLayout.X_AXIS));
buttons.add (playButton);
buttons.add (Box.createRigidArea (new Dimension(5,0)));
buttons.add (stopButton);
setPreferredSize (new Dimension (350, 200));
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
add (Box.createRigidArea (new Dimension(0,5)));
add (titleLabel);
add (Box.createRigidArea (new Dimension(0,5)));
add (musicCombo);
add (Box.createRigidArea (new Dimension(0,5)));
add (buttons);
add (Box.createRigidArea (new Dimension(0,5)));
musicCombo.addActionListener (new ComboListener());
stopButton.addActionListener (new ButtonListener());
playButton.addActionListener (new ButtonListener());
current = null;
}
}
private class ComboListener implements ActionListener {
public void actionPerformed (ActionEvent event)
{
if (current != null)
current.stop();
current = music[musicCombo.getSelectedIndex()];
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event)
{
if (current != null)
current.stop();
if (event.getSource() == playButton)
if (current != null)
current.play();
}
}
}
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 :
LaunchingWindow :
MusicContainer:
MusicListener :
PlayingMusic :
StoppingMusic :
If you were to use
javax.sound.sampled.Clip
and the supporting API instead ofjava.applet.AudioClip
, you would gain much more control over the audio clip, for example...Basically, when playing and you press the "pause" button, the
ActionListener
gets the current playback frame and stops the clip, when you click theplay
buttonActionListener
will set the clip to the current frame and resume playback