我的程序中有一个按钮,可以启用或禁用声音。我尝试使用此代码,但我只能启动剪辑。你有什么建议吗?
if (control == view.btnSound) {
try {
URL url = this.getClass().getResource("test.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
final Clip clip = AudioSystem.getClip();
clip.open(audioIn);
if (view.btnSound.isSelected()) {
clip.start();
view.btnSound.setText("Sound ein");
//clip.loop(clip.LOOP_CONTINUOUSLY);
}
else {
clip.stop();
view.btnSound.setText("Sound aus");
}
} catch (Exception e) {
System.out.println("Error with playing sound.");
e.printStackTrace();
}
}