我可能接近这个错误,但我需要找出如何停止循环javax.sound.sampled剪辑。我有9种不同的声音。当用户按下增大幅度按钮时,我想要播放不同的声音。目前我正在调用playSound方法,每次单击按钮并且它正在工作,但是它不会停止已经播放的声音。声音只是相互影响。
当用户按下按钮时,有没有办法关闭所有现有声音?
这是我的playSound代码:
public void playSound(){
try {
audio = AudioSystem.getAudioInputStream(soundFile[activeSound]);
clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
catch (IOException ex){
System.out.println("Sorry but there has been a problem reading your file.");
ex.printStackTrace();
}
catch (UnsupportedAudioFileException ex1){
System.out.println("Sorry but the audio file format you are using is not supported.");
ex1.printStackTrace();
}
catch (LineUnavailableException ex2){
System.out.println("Sorry but there are audio line problems.");
ex2.printStackTrace();
}
}
我已经在这两天了,这让我很生气。任何帮助将不胜感激。
答案 0 :(得分:1)
您想要的是停止播放所有现有剪辑。这可以使用Dataline.stop()方法完成。您所需要的只是能够访问所有现有剪辑。以下是我的建议。请注意,我只使用一个引用链接到当前循环剪辑。如果您有多个,请使用ArrayList<Clip>
而不是仅使用一个。
private Clip activeClip;
public void playSound(){
activeClip.stop();
try {
audio = AudioSystem.getAudioInputStream(soundFile[activeSound]);
clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
activeClip = clip;
}
catch (IOException ex){
System.out.println("Sorry but there has been a problem reading your file.");
ex.printStackTrace();
}
catch (UnsupportedAudioFileException ex1){
System.out.println("Sorry but the audio file format you are using is not supported.");
ex1.printStackTrace();
}
catch (LineUnavailableException ex2){
System.out.println("Sorry but there are audio line problems.");
ex2.printStackTrace();
}
}