我正在制作一个游戏,一个具体的危险游戏,并且在游戏中我有背景噪音,例如,如果用户得到正确的问题,就会发出叮当声,人群欢呼。这不是一个巨大的游戏破解问题,但我想知道如果用户在提示的JOptionPane中单击“确定”,是否有任何方法可以阻止噪音。我还想说我对编码很新。
我已经尝试了如何停止正在播放的文件然而我无法找到任何东西,所以如果有人知道如何制作它所以要么正在播放的文件可以被停止或者文件被保存在其中的方法可以是停下来会很棒。
package testing;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import static java.awt.Font.BOLD;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class testing implements ActionListener{
static String Choice;
static String[][] Questions = {{"The Raptor is the mascot for which basketball team"}};
static String[][] Answers = {{"Toronto Raptors"}};
public JButton[][] t = new JButton[5][5];
public static void main(String[] args) {
new testing();
}
static int n = 100;
public testing() {
JFrame scoreframe = new JFrame("SCORE");
scoreframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scoreframe.setSize(400,250);
scoreframe.setVisible(true);
JFrame gameframe = new JFrame("Jeopardy");
gameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameframe.setSize(1920,1080);
gameframe.setLayout(new GridLayout(6, 4));
for (int r = 0; r < 5; r++) {
for (int c = 0; c < 5; c++) {
String vakue = String.valueOf(n);
t[r][c] = new JButton(vakue);
t[r][c].setBackground(Color.BLUE);
t[r][c].setForeground(Color.YELLOW);
t[r][c].putClientProperty("rows", r);
t[r][c].putClientProperty("columns", c);
t[r][c].setFont(new Font("Swis721 BlkEx BT", BOLD, 40));
t[r][c].addActionListener(this);
gameframe.add(t[r][c]);
}
n = n +300;
gameframe.setVisible(true);
}
}
public static void crowd() {
try {
File file = new File( "crowd.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.start();
} catch (Exception e) {
}
}
public static void applause() {
try {
File file = new File( "Applause.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.start();
} catch (Exception e) {
}
}
public static void check() {
try {
File file = new File( "check.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.start();
} catch (Exception e) {
}
}
@Override
public void actionPerformed(ActionEvent e) {
JButton A = new JButton();
A = (JButton) e.getSource();
int rows = (int) A.getClientProperty("rows");
int cols = (int) A.getClientProperty("columns");
Choice = JOptionPane.showInputDialog(null, Questions[rows][cols]);
String score = A.getText();
A.addActionListener(this);
if (Choice.equalsIgnoreCase(Answers[rows][cols])) {
check();
applause();
crowd();
JOptionPane.showMessageDialog(null, "That's right! The correct answer was: " + Answers[rows][cols]+"! \nYou win " + score + "$");
A.setEnabled(false);
}
}
}
我缩小了我的代码,所以如果有人试图在他们的程序中尝试它,他们就不必处理任何额外的事情。基本上,我打破了我的代码以显示问题,我知道你不能听到声音但是为了帮助目的,让我们只说声音都是10分钟(他们不是真的,但让我们说它们是)如果你运行我的代码并点击位置1,1的第一个按钮,它将询问你的猛禽是什么篮球队的吉祥物,所以当你输入答案(“多伦多猛龙队”)时会出现一条消息告诉用户他们有问题正确,他们赚了多少钱,因为这发生了10分钟的音乐在后面播放。他们点击“确定”并且音乐保留在整个音轨的后面,我怎么能这样做当用户点击“确定”声音停止?
首先,您想要保留对当前剪辑的引用(无论它现在是否正在播放都无关紧要)。为此,创建一个包含剪辑的静态变量。然后定义play
和stop
函数,当每个play
将首先尝试stop
。此外,最好不要在不同的函数中复制和粘贴相同的代码:
private static Clip clip = null;
public static void stop() {
if (clip != null) {
clip.stop();
clip = null;
}
}
public static void play(String toPlayName) {
stop();
File toPlay = new File(toPlayName);
try {
clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(toPlay));
clip.start();
} catch (Exception e) {
}
}
public static void crowd() {
play("crowd.wav");
}
public static void applause() {
play("Applause.wav");
}
public static void check() {
play("check.wav");
}