fix for bug #208, sound not playing the second time around

This commit is contained in:
benfry
2005-11-30 18:39:31 +00:00
parent 57ede20f53
commit 1a0b143f81
2 changed files with 28 additions and 5 deletions
+25 -1
View File
@@ -41,8 +41,9 @@ import javax.sound.sampled.*;
* Java Alamanac</A>
*/
public class PSound2 extends PSound {
Clip clip;
public Clip clip;
FloatControl gainControl;
boolean stopCalled;
public PSound2(PApplet iparent, InputStream input) {
@@ -111,6 +112,22 @@ public class PSound2 extends PSound {
clip.addLineListener(new LineListener() {
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
if (!stopCalled) {
// if the stop() method was called, then the clip
// is already being stopped, and stopping it here will
// may pre-empt it from being immediately restarted.
// i.e. with the code sound.stop(); sound.play();
if (playing()) {
// when playing, needs to shut everything off
clip.stop();
clip.setFramePosition(0);
} else {
// if not playing, just needs to reset things
clip.stop();
}
}
stopCalled = false;
try {
soundEventMethod.invoke(parent,
new Object[] { PSound2.this });
@@ -138,6 +155,11 @@ public class PSound2 extends PSound {
}
public boolean playing() {
return clip.isActive();
}
/**
* either sets repeat flag, or begins playing (and sets)
*/
@@ -170,11 +192,13 @@ public class PSound2 extends PSound {
* Stops the audio and rewinds to the beginning.
*/
public void stop() {
//System.out.println("method stop");
// clip may become null in the midst of this method
//if (clip != null) clip.stop();
//if (clip != null) clip.setFramePosition(0);
clip.stop();
clip.setFramePosition(0);
stopCalled = true;
}