Skip to content

Commit e87bfde

Browse files
committed
Avoid waiting on a closed TrackHandler
1 parent 5245203 commit e87bfde

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/player/Player.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public void crossfadeNextTrack(@NotNull TrackHandler handler, @Nullable String u
315315
if (uri != null && !next.toSpotifyUri().equals(uri))
316316
LOGGER.warn(String.format("Fade out track URI is different from next track URI! {next: %s, crossfade: %s}", next, uri));
317317

318-
if (preloadTrackHandler != null && preloadTrackHandler.isTrack(next)) {
318+
if (preloadTrackHandler != null && preloadTrackHandler.isPlayable(next)) {
319319
crossfadeHandler = preloadTrackHandler;
320320
} else {
321321
LOGGER.warn("Did not preload crossfade track. That's bad.");
@@ -402,7 +402,7 @@ private void loadTrack(boolean play, @NotNull PushToMixerReason reason) {
402402
}
403403

404404
PlayableId id = state.getCurrentPlayableOrThrow();
405-
if (crossfadeHandler != null && crossfadeHandler.isTrack(id)) {
405+
if (crossfadeHandler != null && crossfadeHandler.isPlayable(id)) {
406406
trackHandler = crossfadeHandler;
407407
if (preloadTrackHandler == crossfadeHandler) preloadTrackHandler = null;
408408
crossfadeHandler = null;
@@ -429,7 +429,7 @@ private void loadTrack(boolean play, @NotNull PushToMixerReason reason) {
429429
events.dispatchPlaybackResumed();
430430
}
431431
} else {
432-
if (preloadTrackHandler != null && preloadTrackHandler.isTrack(id)) {
432+
if (preloadTrackHandler != null && preloadTrackHandler.isPlayable(id)) {
433433
trackHandler = preloadTrackHandler;
434434
preloadTrackHandler = null;
435435

@@ -732,7 +732,7 @@ void dispatchTrackChanged() {
732732

733733
Metadata.Track track;
734734
Metadata.Episode episode;
735-
if (trackHandler.isTrack(id)) {
735+
if (trackHandler.isPlayable(id)) {
736736
track = trackHandler.track();
737737
episode = trackHandler.episode();
738738
} else {

core/src/main/java/xyz/gianlu/librespot/player/PlayerRunner.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,13 @@ public void run() {
442442
handler.clearOut();
443443
break;
444444
case Stop:
445-
handler = loadedTracks.remove(cmd.id);
445+
handler = loadedTracks.get(cmd.id);
446446
if (handler != null) handler.close();
447447
break;
448448
case Seek:
449449
handler = loadedTracks.get(cmd.id);
450+
if (handler == null) break;
451+
450452
if (!handler.isReady())
451453
handler.waitReady();
452454

@@ -546,6 +548,7 @@ private void clearOut() {
546548
}
547549

548550
boolean isReady() {
551+
if (closed) throw new IllegalStateException("The handler is closed!");
549552
return codec != null;
550553
}
551554

@@ -656,12 +659,13 @@ public void close() {
656659
try {
657660
clearOut();
658661
if (codec != null) codec.close();
662+
codec = null;
659663
} catch (IOException ignored) {
660664
}
661665
}
662666

663-
boolean isTrack(@NotNull PlayableId id) {
664-
return playable.toSpotifyUri().equals(id.toSpotifyUri());
667+
boolean isPlayable(@NotNull PlayableId id) {
668+
return !closed && playable.toSpotifyUri().equals(id.toSpotifyUri());
665669
}
666670

667671
void seek(int pos) {

0 commit comments

Comments
 (0)