I am seeing some strange behaviour with Clip
instances in Java.
The purpose of the class I am working on is to keep count of the number of Clip
instances containing the same sound sample (indexed by URI
.) When the application requests to play a clip and there are already three or more clips from the same source already playing, the following steps are performed:
- Sort the currently-playing clips by a weighted sum of
PAN
andframePosition
. - Select the clip with the highest value as the one to be stopped and re-started.
- Re-start the clip (the following method):
void restart(Clip clip, float gain, float pan) {
clip.stop();
clip.flush();
pan = Math.max(-1f, Math.min(pan, 1f));
((FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN))
.setValue(gain);
((FloatControl) clip.getControl(FloatControl.Type.PAN))
.setValue(pan);
clip.setFramePosition(0);
clip.start();
}
Strange behaviour occurs if this method is called many times in quick succession (e.g. 20 times within 1ms):
- The clip plays
- The clip raises
LineEvent.Type.START
to signal that it has started playing - The clip never raises
LineEvent.Type.STOP
- getFramePosition returns
0
indefinitely.
Any idea what could be causing this?
I don't think it's a threading issue. Only one thread is calling the public methods of my class (and they are all synchronized
anyway)