Fix minor NPE glitch on music playback

This commit is contained in:
Brendan Robert 2024-02-26 20:38:11 -06:00
parent 7a200d8130
commit 2651818ca2
2 changed files with 5 additions and 8 deletions

View File

@ -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;
}
});
}

View File

@ -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();