Skip to content

Commit

Permalink
Wait that the audio thread has cleaned up the audio resources
Browse files Browse the repository at this point in the history
  • Loading branch information
tonihele committed Sep 29, 2024
1 parent e2ecdd5 commit c2433a9
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/toniarts/openkeeper/video/TgqPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.file.Path;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
Expand All @@ -46,7 +47,7 @@
public abstract class TgqPlayer {

private static final Logger logger = System.getLogger(TgqPlayer.class.getName());

private final Path file;
private static final int FPS = 25; // The specs say 15 FPS, but with this they are totally in sync, dunno why
private static final int FRAME_INTERVAL = (int) Math.floor(1000 / FPS); // In milliseconds
Expand All @@ -63,6 +64,12 @@ public abstract class TgqPlayer {
private SourceDataLine line;
private final Object bufferedEvent = new Object();
private final Object audioHeaderEvent = new Object();

/**
* We need to wait the audio to be cleaned up properly or otherwise other
* things may fail to init it after us
*/
private final CountDownLatch countDownLatch = new CountDownLatch(1);
private boolean stopped = true;

public TgqPlayer(Path file) {
Expand Down Expand Up @@ -119,6 +126,15 @@ public synchronized void stop() {
if (audioPlaybackThread != null && audioPlaybackThread.isAlive()) {
audioPlaybackThread.interrupt();
}

// Wait for the threads to die down
if (countDownLatch.getCount() != 0) {
try {
countDownLatch.await();
} catch (InterruptedException ex) {
logger.log(Level.WARNING, "Interrupted while waiting for count down latch", ex);
}
}
} finally {

// And finally notify, just call this once
Expand Down Expand Up @@ -249,6 +265,7 @@ public void run() {
}

// Make sure we call stop at the end
countDownLatch.countDown();
stop();
}
}
Expand Down

0 comments on commit c2433a9

Please sign in to comment.