55import de .labystudio .spotifyapi .model .Track ;
66import de .labystudio .spotifyapi .platform .AbstractTickSpotifyAPI ;
77import de .labystudio .spotifyapi .platform .linux .api .MPRISCommunicator ;
8+ import de .labystudio .spotifyapi .platform .linux .api .model .Metadata ;
89
10+ import javax .imageio .ImageIO ;
11+ import java .awt .image .BufferedImage ;
12+ import java .net .URL ;
913import java .util .Objects ;
1014
1115/**
@@ -29,23 +33,26 @@ public class LinuxSpotifyApi extends AbstractTickSpotifyAPI {
2933
3034 @ Override
3135 protected void onTick () throws Exception {
32- String trackId = this .mediaPlayer .getTrackId ();
36+ Metadata metadata = this .mediaPlayer .readMetadata ();
37+ String trackId = metadata .getTrackId ();
3338
3439 // Handle on connect
35- if (!this .connected && ! trackId . isEmpty () ) {
40+ if (!this .connected ) {
3641 this .connected = true ;
3742 this .listeners .forEach (SpotifyListener ::onConnect );
3843 }
3944
4045 // Handle track changes
41- if (!Objects .equals (trackId , this .currentTrack == null ? null : this .currentTrack .getId ())) {
42- String trackName = this .mediaPlayer .getTrackName ();
43- String trackArtist = this .mediaPlayer .getArtist ();
44- int trackLength = this .mediaPlayer .getTrackLength ();
46+ String currentTrackId = this .currentTrack == null ? null : this .currentTrack .getId ();
47+ if (!Objects .equals (trackId , currentTrackId )) {
48+ String trackName = metadata .getTrackName ();
49+ String trackArtist = metadata .getArtistsJoined ();
50+ int trackLength = metadata .getTrackLength ();
51+ BufferedImage coverArt = this .toBufferedImage (metadata .getArtUrl ());
4552
4653 boolean isFirstTrack = !this .hasTrack ();
4754
48- Track track = new Track (trackId , trackName , trackArtist , trackLength );
55+ Track track = new Track (trackId , trackName , trackArtist , trackLength , coverArt );
4956 this .currentTrack = track ;
5057
5158 // Fire on track changed
@@ -58,7 +65,7 @@ protected void onTick() throws Exception {
5865 }
5966
6067 // Handle is playing changes
61- boolean isPlaying = this .mediaPlayer .isPlaying ();
68+ boolean isPlaying = this .mediaPlayer .readIsPlaying ();
6269 if (isPlaying != this .isPlaying ) {
6370 this .isPlaying = isPlaying ;
6471
@@ -67,7 +74,7 @@ protected void onTick() throws Exception {
6774 }
6875
6976 // Handle position changes
70- int position = this .mediaPlayer .getPosition ();
77+ int position = this .mediaPlayer .readPosition ();
7178 if (!this .hasPosition () || Math .abs (position - this .getPosition ()) >= 1000 ) {
7279 this .updatePosition (position );
7380 }
@@ -150,4 +157,16 @@ public boolean hasPosition() {
150157 return this .currentPosition != -1 ;
151158 }
152159
160+ private BufferedImage toBufferedImage (String artUrl ) {
161+ if (artUrl == null || artUrl .isEmpty ()) {
162+ return null ; // No cover art available
163+ }
164+ try {
165+ return ImageIO .read (new URL (artUrl ));
166+ } catch (Throwable e ) {
167+ e .printStackTrace ();
168+ return null ; // Failed to load cover art
169+ }
170+ }
171+
153172}
0 commit comments