Prevent null pointer with getting media time

This commit is contained in:
Brendan Robert 2024-02-25 21:10:39 -06:00
parent 56e524c9ad
commit f9321da4cd

View File

@ -17,6 +17,7 @@ public class MediaPlayer {
Status status = Status.NOT_STARTED; Status status = Status.NOT_STARTED;
Media soundData; Media soundData;
SoundBuffer playbackBuffer; SoundBuffer playbackBuffer;
Duration lastKnownDuration = Duration.ZERO;
Executor executor = Executors.newSingleThreadExecutor(); Executor executor = Executors.newSingleThreadExecutor();
public static enum Status { public static enum Status {
@ -34,7 +35,11 @@ public class MediaPlayer {
} }
public Duration getCurrentTime() { public Duration getCurrentTime() {
return soundData.getCurrentTime(); if (soundData == null) {
return lastKnownDuration;
} else {
return soundData.getCurrentTime();
}
} }
public double getVolume() { public double getVolume() {
@ -54,6 +59,7 @@ public class MediaPlayer {
// Ignore exception on shutdown // Ignore exception on shutdown
} finally { } finally {
if (soundData != null) { if (soundData != null) {
lastKnownDuration = soundData.getCurrentTime();
soundData.close(); soundData.close();
} }
soundData = null; soundData = null;