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();
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…