Skip to content

Commit 62f1a46

Browse files
committed
improve playback address search, prevent position interpolation above track length, version 1.1.4
1 parent 3b48042 commit 62f1a46

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repositories {
2323
}
2424
2525
dependencies {
26-
implementation 'com.github.LabyStudio:java-spotify-api:1.1.3:all'
26+
implementation 'com.github.LabyStudio:java-spotify-api:1.1.4:all'
2727
}
2828
```
2929

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'de.labystudio'
7-
version '1.1.3'
7+
version '1.1.4'
88

99
compileJava {
1010
sourceCompatibility = '1.8'

src/main/java/de/labystudio/spotifyapi/platform/windows/WinSpotifyAPI.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class WinSpotifyAPI extends AbstractTickSpotifyAPI {
2929
private long lastTimePositionUpdated;
3030
private boolean positionKnown = false;
3131
private long lastAccessorPosition = -1;
32+
private long lastTimeSynced;
3233

3334
/**
3435
* Updates the current track, position and playback state.
@@ -55,6 +56,13 @@ protected void onTick() {
5556

5657
// Handle track changes
5758
if (!Objects.equals(trackId, this.currentTrack == null ? null : this.currentTrack.getId())) {
59+
// Wait two seconds before updating the track, so Spotify has enough time to update the playback
60+
if ((!this.hasTrack() || playback.getLength() == this.currentTrack.getLength())
61+
&& playback.getPosition() >= this.currentPosition
62+
&& System.currentTimeMillis() - this.lastTimeSynced < 2000) {
63+
return;
64+
}
65+
5866
SpotifyTitle title = this.process.getTitle();
5967
if (title != SpotifyTitle.UNKNOWN) {
6068
int trackLength = playback.getLength();
@@ -91,6 +99,7 @@ protected void onTick() {
9199

92100
// Fire keep alive
93101
this.listeners.forEach(SpotifyListener::onSync);
102+
this.lastTimeSynced = System.currentTimeMillis();
94103
} catch (Exception exception) {
95104
// Fire on disconnect
96105
this.listeners.forEach(listener -> listener.onDisconnect(exception));
@@ -132,7 +141,13 @@ public int getPosition() {
132141
if (this.isPlaying) {
133142
// Interpolate position
134143
long timePassed = System.currentTimeMillis() - this.lastTimePositionUpdated;
135-
return this.currentPosition + (int) timePassed;
144+
long interpolatedPosition = this.currentPosition + timePassed;
145+
146+
if (this.hasTrack()) {
147+
return (int) Math.min(interpolatedPosition, this.currentTrack.getLength());
148+
} else {
149+
return (int) interpolatedPosition;
150+
}
136151
} else {
137152
return this.currentPosition;
138153
}

src/main/java/de/labystudio/spotifyapi/platform/windows/api/spotify/SpotifyProcess.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ public SpotifyProcess() {
6666
PREFIX_CONTEXT,
6767
(address, index) -> {
6868
PlaybackAccessor accessor = new PlaybackAccessor(this, address);
69-
return accessor.isValid() && accessor.isPlaying() == isPlaying; // Check if address is valid
69+
boolean valid = accessor.isValid() && accessor.isPlaying() == isPlaying; // Check if address is valid
70+
71+
// If valid then pull the data again and check if it is still valid
72+
if (valid) {
73+
accessor.update();
74+
return accessor.isValid();
75+
}
76+
77+
return false;
7078
}
7179
);
7280
if (this.addressPlayBack == -1) {

0 commit comments

Comments
 (0)