java - 我试图停止当用户继续播放时正在播放的特定方法
问题描述
我正在制作一款游戏,具体来说是一款危险游戏,并且在游戏中我有背景噪音,例如,如果用户答对了问题,就会发出叮当声,人群欢呼。这不是一个巨大的破坏游戏的问题,但是我想知道如果用户在提示的 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");
}
推荐阅读
- elasticsearch - elasticsearch中是否有匹配短语任何查询?
- swift - 调用 Firebase fetch 方法时无法推断通用参数“T”
- javascript - 如何使用 javascript 填充列表组?
- typescript - 打字稿 - 无法写入文件,因为它会覆盖输入文件
- java - 在 Java 中将文本文件转换为 ARFF
- python - 如何在 Django 中创建动态表单域?
- java - 占位符未在 @SpringBootTest 上得到解决
- windows - IMFMediaSource 关闭后,IMFByteStream 变得不可用(MF_E_BYTESTREAM_NOT_SEEKABLE)。使用 Microsoft Media Foundation 播放 mp3 播放列表
- node.js - 如何自定义 Stream 转换以解析流数组 json
- java - java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils 尝试使用 gradle 运行 mockito junit5 测试