Skip to content

Commit 9d38b79

Browse files
committed
fallback to pseudo playback accessor if main accessor is invalid
1 parent e1c03d9 commit 9d38b79

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ protected void onTick() {
4242
this.listeners.forEach(SpotifyListener::onConnect);
4343
}
4444

45-
PlaybackAccessor playback = this.process.getPlaybackAccessor();
46-
String trackId = this.process.getTrackId();
45+
// Read track id and check if track id is valid
46+
String trackId = this.process.readTrackId();
47+
if (!this.process.isTrackIdValid(trackId)) {
48+
throw new IllegalStateException("Invalid track ID: " + trackId);
49+
}
4750

48-
// Update playback status and check if it is valid
49-
if (!this.process.isTrackIdValid(trackId) || !playback.update()) {
50-
this.currentPosition = -1;
51-
throw new IllegalStateException("Could not update playback");
51+
// Update playback state
52+
PlaybackAccessor playback = this.process.getMainPlaybackAccessor();
53+
PlaybackAccessor pseudoPlayback = this.process.getPseudoPlaybackAccessor();
54+
if (!playback.update() && pseudoPlayback.update()) {
55+
playback = pseudoPlayback; // Fallback to pseudo playback if main playback fails
5256
}
5357

5458
// Handle track changes
@@ -139,7 +143,7 @@ public boolean hasPosition() {
139143
return false;
140144
}
141145

142-
PlaybackAccessor playback = this.process.getPlaybackAccessor();
146+
PlaybackAccessor playback = this.process.getMainPlaybackAccessor();
143147
return playback.hasTrackPosition();
144148
}
145149

src/main/java/de/labystudio/spotifyapi/platform/windows/api/playback/source/MediaControlPlaybackAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public boolean update() {
2626
this.playbackPosition = this.mediaControl.getPlaybackPosition();
2727
this.trackDuration = this.mediaControl.getTrackDuration();
2828
this.isPlaying = this.mediaControl.isPlaying();
29-
return true;
29+
return this.isValid();
3030
}
3131

3232
@Override

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class SpotifyProcess extends WinProcess {
3737
};
3838

3939
private final long addressTrackId;
40-
private final PlaybackAccessor playbackAccessor;
40+
private final PlaybackAccessor mainPlaybackAccessor;
41+
private final PseudoPlaybackAccessor pseudoPlaybackAccessor;
4142

4243
private WindowsMediaControl mediaControl;
4344

@@ -61,6 +62,9 @@ public SpotifyProcess(SpotifyConfiguration configuration) {
6162
// Find the track id address in the memory
6263
this.addressTrackId = this.findTrackIdAddress();
6364

65+
// The dumb accessor can only detect the current playing state from the process title
66+
this.pseudoPlaybackAccessor = new PseudoPlaybackAccessor(this);
67+
6468
PlaybackAccessor accessor;
6569
try {
6670
// Initialize natives for Media Control access
@@ -72,10 +76,9 @@ public SpotifyProcess(SpotifyConfiguration configuration) {
7276
e.printStackTrace();
7377

7478
// We can continue without Media Control access but some features may not work
75-
// The dumb accessor can only detect the current playing state from the process title
76-
accessor = new PseudoPlaybackAccessor(this);
79+
accessor = this.pseudoPlaybackAccessor;
7780
}
78-
this.playbackAccessor = accessor;
81+
this.mainPlaybackAccessor = accessor;
7982

8083
if (DEBUG) {
8184
System.out.println("Scanning took " + (System.currentTimeMillis() - timeScanStart) + "ms");
@@ -181,7 +184,7 @@ private String readTrackId(long address) {
181184
*
182185
* @return the track id without the prefix "spotify:track:"
183186
*/
184-
public String getTrackId() {
187+
public String readTrackId() {
185188
return this.readTrackId(this.addressTrackId);
186189
}
187190

@@ -210,8 +213,12 @@ public SpotifyTitle getTitle() {
210213
return (this.previousTitle = title);
211214
}
212215

213-
public PlaybackAccessor getPlaybackAccessor() {
214-
return this.playbackAccessor;
216+
public PlaybackAccessor getMainPlaybackAccessor() {
217+
return this.mainPlaybackAccessor;
218+
}
219+
220+
public PseudoPlaybackAccessor getPseudoPlaybackAccessor() {
221+
return this.pseudoPlaybackAccessor;
215222
}
216223

217224
public long getAddressTrackId() {

0 commit comments

Comments
 (0)