我可能没有正确地处理这个问题,但我需要找出如何停止循环的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();
}
}我已经做了两天了,快把我逼疯了。任何帮助都将不胜感激。
发布于 2012-04-10 20:16:38
您想要的是停止播放所有现有的剪辑。这可以使用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();
}
}https://stackoverflow.com/questions/10088530
复制相似问题