Skip to content

Commit 6f4c166

Browse files
committed
reset position on song change, version 1.0.9
1 parent a32af83 commit 6f4c166

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repositories {
2222
}
2323
2424
dependencies {
25-
implementation 'com.github.LabyStudio:java-spotify-api:1.0.8:all'
25+
implementation 'com.github.LabyStudio:java-spotify-api:1.0.9:all'
2626
}
2727
```
2828

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.0.8'
7+
version '1.0.9'
88

99
compileJava {
1010
sourceCompatibility = '1.8'

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class WinSpotifyAPI extends AbstractSpotifyAPI {
3131

3232
private long lastTimePositionUpdated;
3333
private boolean positionKnown = false;
34+
private long lastAccessorPosition = -1;
3435

3536
private ScheduledFuture<?> task;
3637

@@ -83,12 +84,18 @@ private void onTick() {
8384
SpotifyTitle title = this.process.getTitle();
8485
if (title != SpotifyTitle.UNKNOWN) {
8586
int trackLength = playback.getLength();
87+
boolean isFirstTrack = !this.hasTrack();
8688

8789
Track track = new Track(trackId, title.getTrackName(), title.getTrackArtist(), trackLength);
8890
this.currentTrack = track;
8991

9092
// Fire on track changed
9193
this.listeners.forEach(listener -> listener.onTrackChanged(track));
94+
95+
// Reset position on song change
96+
if (!isFirstTrack) {
97+
this.updatePosition(0);
98+
}
9299
}
93100
}
94101

@@ -103,17 +110,9 @@ private void onTick() {
103110

104111
// Handle position changes
105112
int position = playback.getPosition();
106-
if (position != this.currentPosition) {
107-
// Update position known state
108-
this.positionKnown = this.currentPosition != -1 || !isPlaying;
109-
this.currentPosition = position;
110-
111-
if (this.positionKnown) {
112-
this.lastTimePositionUpdated = System.currentTimeMillis();
113-
114-
// Fire on position changed
115-
this.listeners.forEach(listener -> listener.onPositionChanged(position));
116-
}
113+
if (position != this.lastAccessorPosition) {
114+
this.lastAccessorPosition = position;
115+
this.updatePosition(position);
117116
}
118117

119118
// Fire keep alive
@@ -125,6 +124,23 @@ private void onTick() {
125124
}
126125
}
127126

127+
private void updatePosition(int position) {
128+
if (position == this.currentPosition) {
129+
return;
130+
}
131+
132+
// Update position known state
133+
this.positionKnown = this.currentPosition != -1 || !this.isPlaying;
134+
this.currentPosition = position;
135+
136+
if (this.positionKnown) {
137+
this.lastTimePositionUpdated = System.currentTimeMillis();
138+
139+
// Fire on position changed
140+
this.listeners.forEach(listener -> listener.onPositionChanged(position));
141+
}
142+
}
143+
128144
@Override
129145
public Track getTrack() {
130146
return this.currentTrack;

0 commit comments

Comments
 (0)