I found many topics suggesting and discussing cloning but I have been unable to actually implement a method which can duplicate my Clip object.
Here is what I have tried:
// ... setting up class ...
MyClip GunClip = new MyClip();
GunClip.set(AudioSystem.getClip());
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(getClass().getResourceAsStream("/Resources/sound/Laser.wav")));
GunClip.dummy.open(inputStream);
// ...
Then later when an event is triggered I want to play that sound repeatedly. So I try to duplicate it:
class MyClip implements Cloneable {
Clip dummy;
public MyClip() {
}
public Clip get() {
return dummy;
}
public void set(Clip c) {
this.dummy = c;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
I implement the Cloneable class as suggested in this topic.
And then I clone it before I play it:
MyClip c = (MyClip) GunClip.clone();
c.dummy.setFramePosition(0);
c.dummy.start();
But even now it doesn't work...
EDIT: I have figured out why it doesn't work, which is due to the fact that it is not a deep clone and the InputStream which is being used by the original GunClip is not being cloned. But since Clip is an abstract interface it may be harder than normal to clone it.