samedi 20 décembre 2014

Java Sound only plays once and then wont play again

Hello I have a game that needs to be able to play sound. when I click a button the sound plays but when I click it again the sound wont play this is my sound class:



public class Sound {

public static final Sound select = new Sound("/selectBTN.wav");
public static final Sound scroll = new Sound("/btn.wav");

//Phone calls
public static final Sound call1 = new Sound("/calls/call1.wav");
public static final Sound call2 = new Sound("/calls/call2.wav");
public static final Sound call3 = new Sound("/calls/call3.wav");


private Clip c;

public Sound(final String filename) {
try {
c = AudioSystem.getClip();
final AudioInputStream inputStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(filename));
c.open(inputStream);
c.addLineListener(new LineListener() {

@Override
public void update(final LineEvent event) {
if (event.getType().equals(Type.STOP)) {
System.out.println("Do something");
}
}
});
} catch (final Exception e) {
e.printStackTrace();
}
}

public void play() {
try {
new Thread() {
public void run() {
if (!title.mute) {

c.start();
}
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
}


}


I use this line: Sound.call1.play(); to play the sound. can any one help me to fix this problem thanks in advance:)


Aucun commentaire:

Enregistrer un commentaire