From 2651818ca2a3c6a4b034e27a5309da3887ddc5b2 Mon Sep 17 00:00:00 2001 From: Brendan Robert Date: Mon, 26 Feb 2024 20:38:11 -0600 Subject: [PATCH] Fix minor NPE glitch on music playback --- .../jace/src/main/java/jace/lawless/MediaPlayer.java | 8 +++++--- .../tools/jace/src/test/java/jace/core/SoundTest.java | 5 ----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Platform/Apple/tools/jace/src/main/java/jace/lawless/MediaPlayer.java b/Platform/Apple/tools/jace/src/main/java/jace/lawless/MediaPlayer.java index 96d56a5f..6cdab452 100644 --- a/Platform/Apple/tools/jace/src/main/java/jace/lawless/MediaPlayer.java +++ b/Platform/Apple/tools/jace/src/main/java/jace/lawless/MediaPlayer.java @@ -101,10 +101,11 @@ public class MediaPlayer { } } executor.execute(() -> { + SoundBuffer theBuffer = playbackBuffer; status = Status.PLAYING; // System.out.println("Song playback thread started"); Media theSoundData = soundData; - while (status == Status.PLAYING && (maxRepetitions == INDEFINITE || repeats < maxRepetitions) && theSoundData != null) { + while (status == Status.PLAYING && (maxRepetitions == INDEFINITE || repeats < maxRepetitions) && theSoundData != null && theBuffer != null) { if (theSoundData.isEnded()) { if (maxRepetitions == INDEFINITE) { theSoundData.restart(); @@ -120,13 +121,14 @@ public class MediaPlayer { } } try { - playbackBuffer.playSample((short) (theSoundData.getNextLeftSample() * vol)); - playbackBuffer.playSample((short) (theSoundData.getNextRightSample() * vol)); + theBuffer.playSample((short) (theSoundData.getNextLeftSample() * vol)); + theBuffer.playSample((short) (theSoundData.getNextRightSample() * vol)); } catch (InterruptedException | ExecutionException | SoundError e) { e.printStackTrace(); this.stop(); } theSoundData = soundData; + theBuffer = playbackBuffer; } }); } diff --git a/Platform/Apple/tools/jace/src/test/java/jace/core/SoundTest.java b/Platform/Apple/tools/jace/src/test/java/jace/core/SoundTest.java index 60ae7705..8eaf3d27 100644 --- a/Platform/Apple/tools/jace/src/test/java/jace/core/SoundTest.java +++ b/Platform/Apple/tools/jace/src/test/java/jace/core/SoundTest.java @@ -97,12 +97,7 @@ public class SoundTest extends AbstractFXTest { // @Test /** - * TODO: Fix the playback buffer NPE issue * Runs through 500 iterations of playing a random song for 1 second and switching songs - * Currently, no major issues except that 2% of the time there is this error: - * Exception in thread "pool-36-thread-1" java.lang.NullPointerException: - * Cannot invoke "jace.core.SoundMixer$SoundBuffer.playSample(short)" because "this.playbackBuffer" is null - * at lawlesslegends/jace.lawless.MediaPlayer.lambda$0(MediaPlayer.java:124) */ public void musicPlaybackTortureTest() throws InterruptedException { SoundMixer.initSound();