From 1a0b143f815575c8b715b8b179c916c97a909b74 Mon Sep 17 00:00:00 2001 From: benfry Date: Wed, 30 Nov 2005 18:39:31 +0000 Subject: [PATCH] fix for bug #208, sound not playing the second time around --- core/PSound2.java | 26 +++++++++++++++++++++++++- core/todo.txt | 7 +++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/core/PSound2.java b/core/PSound2.java index 179460ee5..74620dad5 100755 --- a/core/PSound2.java +++ b/core/PSound2.java @@ -41,8 +41,9 @@ import javax.sound.sampled.*; * Java Alamanac */ 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; } diff --git a/core/todo.txt b/core/todo.txt index da0f9c5ea..007b19396 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -6,10 +6,9 @@ X probably only set when resize() is called, and it's not happening X be careful when fixing this, bugs 195/197 were a result: X http://dev.processing.org/bugs/show_bug.cgi?id=195 X http://dev.processing.org/bugs/show_bug.cgi?id=197 - -_ PSound.play() won't play the sound a 2nd time -_ (have to call stop() first) -_ http://dev.processing.org/bugs/show_bug.cgi?id=209 +X PSound.play() won't play the sound a 2nd time +X (have to call stop() first) +X http://dev.processing.org/bugs/show_bug.cgi?id=208 _ make dxf writer that'll work with recordRaw() _ enable PGraphicsPDF for inclusion