From a24ee02c707604441cb0a3e3ae3dda78e9700171 Mon Sep 17 00:00:00 2001 From: werwolf2303 <66752411+werwolf2303@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:36:39 +0200 Subject: [PATCH] Fix login, Fix invalid login, Implement unofficial fixes Implemented unofficial fix: https://github.com/librespot-org/librespot-java/pull/720 --- .../xyz/gianlu/librespot/player/Player.java | 39 ++++++++++++++++++- .../player/playback/PlayerSession.java | 27 ++++++++++++- .../com/spotifyxp/dialogs/LoginDialog.java | 3 +- .../java/com/spotifyxp/utils/PlayerUtils.java | 8 +++- 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/player/Player.java b/src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/player/Player.java index ea21f524..fe6a0e5f 100644 --- a/src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/player/Player.java +++ b/src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/player/Player.java @@ -458,7 +458,8 @@ public void playbackResumedFromHalt(int chunk, long diff) { @Override public @Nullable PlayableId nextPlayable() { - NextPlayable next = state.nextPlayable(conf.autoplayEnabled); + //Prevent exeception when next queue + /*NextPlayable next = state.nextPlayable(conf.autoplayEnabled); if (next == NextPlayable.AUTOPLAY) { loadAutoplay(); return null; @@ -468,6 +469,42 @@ public void playbackResumedFromHalt(int chunk, long diff) { if (next != NextPlayable.OK_PLAY && next != NextPlayable.OK_REPEAT) sink.pause(false); + return state.getCurrentPlayableOrThrow(); + } else { + ConsoleLoggingModules.error("Failed loading next song: " + next); + panicState(PlaybackMetrics.Reason.END_PLAY); + return null; + }*/ + NextPlayable next = state.nextPlayable(conf.autoplayEnabled); + if (next == NextPlayable.AUTOPLAY) { + loadAutoplay(); + return null; + } + + if (next.isOk()) { + if (next != NextPlayable.OK_PLAY && next != NextPlayable.OK_REPEAT) { + /* + 10/1/2023 tagdara - + This is an attempt to remediate librespot moving to the next track in the queue when there is no next track + in the queue. In adddition to re-queueing the current track, this introduces several race conditions when another + track replaces it, causing metadata conflicts and multiple playbackEnded events. + + This is a two part change, which also requires a change in the PlayerSession advanceTo function. + + Removing the pause and returning null instead of returning state.getCurrentPlayableOrThrow() (the same track) as the + next playable prevents player confusion about what track is truly up next. + + This might have an impact on clicking play again on the only track in the queue, but have not experienced this side effect + in testing. + + sink.pause(false); + return state.getCurrentPlayableOrThrow(); + + */ + ConsoleLoggingModules.info("PLAYER.NEXTPLAYABLE - [CODE CHANGE TEST] - sending null on pause to prevent advance"); + return null; + } + return state.getCurrentPlayableOrThrow(); } else { ConsoleLoggingModules.error("Failed loading next song: " + next); diff --git a/src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/player/playback/PlayerSession.java b/src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/player/playback/PlayerSession.java index b99b85a2..6d0d222f 100644 --- a/src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/player/playback/PlayerSession.java +++ b/src/main/java/com/spotifyxp/deps/xyz/gianlu/librespot/player/playback/PlayerSession.java @@ -107,7 +107,7 @@ private void addNext() { * @return Whether the operation was successful */ private boolean advanceTo(@NotNull PlayableId id) { - do { + /*do { PlayerQueueEntry entry = queue.head(); if (entry == null) return false; if (entry.playable.equals(id)) { @@ -116,6 +116,31 @@ private boolean advanceTo(@NotNull PlayableId id) { return true; } } while (queue.advance()); + return false; */ + do { + PlayerQueueEntry entry = queue.head(); + if (entry == null) return false; + if (entry.playable.equals(id)) { + PlayerQueueEntry next = queue.next(); + /* + 10/1/2023 tagdara - + This is an attempt to remediate librespot moving to the next track in the queue when there is no next track + in the queue. In adddition to re-queueing the current track, this introduces several race conditions when another + track replaces it, causing metadata conflicts and multiple playbackEnded events. + + This is a two part change, which also requires a modification to nextPlayable in the Player object. + + In the 1.64 code, if next === null, indicating there is no next track in the queue, it was returning true. Instead + since AdvanceTo should not advance to a non-existent next track, we will now return false. + */ + if (next == null) { + ConsoleLoggingModules.info("PLAYERSESSION.advanceTo {}. queue head is already {} and next is null: {}. [CODE CHANGE] returning false", id, next); + return false; + } + if (!next.playable.equals(id)) + return true; + } + } while (queue.advance()); return false; } diff --git a/src/main/java/com/spotifyxp/dialogs/LoginDialog.java b/src/main/java/com/spotifyxp/dialogs/LoginDialog.java index 228c7da6..ef7f4977 100644 --- a/src/main/java/com/spotifyxp/dialogs/LoginDialog.java +++ b/src/main/java/com/spotifyxp/dialogs/LoginDialog.java @@ -78,6 +78,7 @@ public void openWithInvalidAuth() { public void actionPerformed(ActionEvent e) { PublicValues.config.write(ConfigValues.username.name, spotifyusernamefield.getText()); PublicValues.config.write(ConfigValues.password.name, usernamepasswordfield.getText()); + PublicValues.config.write(ConfigValues.facebook.name, "false"); dialog.dispose(); } }); @@ -132,7 +133,7 @@ public void open() { public void actionPerformed(ActionEvent e) { PublicValues.config.write(ConfigValues.username.name, spotifyusernamefield.getText()); PublicValues.config.write(ConfigValues.password.name, usernamepasswordfield.getText()); - PublicValues.config.write(ConfigValues.facebook.name, "true"); + PublicValues.config.write(ConfigValues.facebook.name, "false"); dialog.dispose(); } }); diff --git a/src/main/java/com/spotifyxp/utils/PlayerUtils.java b/src/main/java/com/spotifyxp/utils/PlayerUtils.java index 3e89461c..583f6961 100644 --- a/src/main/java/com/spotifyxp/utils/PlayerUtils.java +++ b/src/main/java/com/spotifyxp/utils/PlayerUtils.java @@ -3,6 +3,7 @@ import com.spotifyxp.deps.com.spotify.connectstate.Connect; import com.spotifyxp.PublicValues; import com.spotifyxp.configuration.ConfigValues; +import com.spotifyxp.deps.se.michaelthelin.spotify.SpotifyApi; import com.spotifyxp.deps.xyz.gianlu.librespot.audio.decoders.AudioQuality; import com.spotifyxp.deps.xyz.gianlu.librespot.core.Session; import com.spotifyxp.deps.xyz.gianlu.librespot.player.Player; @@ -44,14 +45,17 @@ public static Player buildPlayer() throws EOFException { .build(); try { Session session; - if(PublicValues.config.get(ConfigValues.facebook.name).equalsIgnoreCase("false") || PublicValues.config.get(ConfigValues.facebook.name).equalsIgnoreCase("")) { + if (PublicValues.config.get(ConfigValues.facebook.name).equalsIgnoreCase("false") || PublicValues.config.get(ConfigValues.facebook.name).equalsIgnoreCase("")) { session = builder.userPass(PublicValues.config.get(ConfigValues.username.name), PublicValues.config.get(ConfigValues.password.name)).create(); - }else{ + } else { session = builder.facebook().create(); } Player player = new Player(playerconfig, session); PublicValues.session = session; return player; + }catch (Session.SpotifyAuthenticationException e) { + new LoginDialog().openWithInvalidAuth(); + return buildPlayer(); }catch(Exception e) { e.printStackTrace(); ConsoleLogging.Throwable(e);