我想在执行按钮时播放相同的声音,当我在两次调用此方法时,它仅在第一个按钮中播放,第二个按钮未播放。
这是代码
new Thread(new Runnable()
{
@Override
public void run()
{
// Take the path of the audio file from command line
try
{
File f=new File(this.getClass().getResource("/sounds/Attention.wav").getFile());
// Create a Player object that realizes the audio
final Player p=Manager.createPlayer(f.toURI().toURL());
// Start the music
p.start();
try
{
Thread.sleep(100);
} catch (Exception e) {
}
p.stop();
Thread.interrupted();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
答案 0 :(得分:0)
我也有同样的问题开发游戏。请注意我正在使用 AudioInputStream 和 Clip 条款,但此解决方案对我有用:
protected synchronized void play(Clip clip){
try{
if(clip.isOpen()){
System.out.println("clip is opened...");
clip.start();
clip.setMicrosecondPosition(0); //like rewinding the clip
try {Thread.sleep(10);}
catch (Exception e) {}
}
System.out.println("shooting");
}
catch(Exception e){e.printStackTrace();}
}