每次用户按下或按下时,我在菜单屏幕上播放Clip
。我通过每次将剪辑设置为开头摆脱了唯一的播放问题,但有时(我没有看到模式),剪辑将无法播放。我该如何解决这个问题?
package com.cgp.pong;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
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.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Menu extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private Thread thread;
private File pongf = new File("pong.png");
private BufferedImage pong, titlecontrols, single, two, controls, singley, twoy, controlsy;
private File titlecontrolsf = new File("titlecontrols.png");
private File singlef = new File("single.png");
private File twof = new File("two.png");
private File controlsf = new File("controls.png");
private File singleyf = new File("singley.png");
private File twoyf = new File("twoy.png");
private File controlsyf = new File("controlsy.png");
private boolean[] selected = { true, false, false };
private int selectedint = 0;
private Clip clip;
private AudioInputStream streamwall;
public Menu() {
super();
}
public void run() {
images();
bind();
sound();
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
setCursor(blankCursor);
while (true) {
repaint();
}
}
private void sound() {
File wall = new File("wall.wav");
try {
streamwall = AudioSystem.getAudioInputStream(wall);
} catch (UnsupportedAudioFileException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
DataLine.Info info = new DataLine.Info(Clip.class, streamwall.getFormat());
try {
clip = (Clip) AudioSystem.getLine(info);
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
try {
clip.open(streamwall);
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void bind() {
InputMap im = getInputMap();
ActionMap am = getActionMap();
requestFocus();
setInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, getInputMap());
im.put(KeyStroke.getKeyStroke("DOWN"), "down");
am.put("down", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
clip.start();
clip.setMicrosecondPosition(0);
selected[selectedint] = false;
selectedint++;
if (selectedint > 2)
selectedint = 0;
selected[selectedint] = true;
}
});
im.put(KeyStroke.getKeyStroke("UP"), "up");
am.put("up", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
selected[selectedint] = false;
selectedint--;
if (selectedint < 0)
selectedint = 2;
selected[selectedint] = true;
}
});
im.put(KeyStroke.getKeyStroke("SPACE"), "select");
am.put("select", new AbstractAction() {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent arg0) {
Pong.startGame();
}
});
}
private void images() {
try {
pong = ImageIO.read(pongf);
titlecontrols = ImageIO.read(titlecontrolsf);
single = ImageIO.read(singlef);
two = ImageIO.read(twof);
controls = ImageIO.read(controlsf);
singley = ImageIO.read(singleyf);
twoy = ImageIO.read(twoyf);
controlsy = ImageIO.read(controlsyf);
} catch (IOException e) {
e.printStackTrace();
}
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g) {
super.paint(g);
setBackground(Color.BLACK);
g.setColor(Color.WHITE);
g.drawImage(pong, 150, 0, null);
g.drawImage(titlecontrols, 80, 150, null);
if (selected[0]) {
g.drawImage(singley, 140, 250, null);
} else if (!selected[0]) {
g.drawImage(single, 140, 250, null);
}
if (selected[1]) {
g.drawImage(twoy, 140, 330, null);
} else if (!selected[1]) {
g.drawImage(two, 140, 330, null);
}
if (selected[2]) {
g.drawImage(controlsy, 140, 410, null);
} else if (!selected[2]) {
g.drawImage(controls, 140, 410, null);
}
}
}
提前致谢!